Firebase事件监听器从未触发过

时间:2018-01-31 16:38:03

标签: android firebase-realtime-database listener

我正在尝试从Firebase中检索一些数据,但它不适用于我的某个节点

请注意,UnverifiedEmployees节点工作正常但“公司”节点不正常 我能够在事件监听器之前看到Log RIGHT,但它内部没有打印出来 我指的是这个区域:

public void addDataToFirebase() {

                Log.i("BEFORE", "BEFORE COMPANIES REF");
                companiesRef.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        Log.i("COMPANIESREF", "INSIDE COMPANIES REF ONDATACHANGE");

我在另一部手机上试过这个。我已经尝试过wifi和4g。我也尝试过登录其他用户 我也尝试从手机上卸载应用程序并重新安装。我尝试过编写addListener的不同实现。 我也尝试过查找,但没有找到很多(例如Android Firebase addListenerForSingleValueEvent not called)(Firebase Android addListenerForSingleValueEvent sometimes not returning data

数据库:(如果存在UserID,请抓取与其相关的CompanyID)

Database Structure

代码的完整性:AddEmployeeActivity.java:

    public class AddEmployeeActivity extends Activity {

        private EditText firstNameET, lastNameET, phoneNumberET, emailET, ssnET;
        private ImageView checkmarkImage;

        private static FirebaseUser currentUser;
        private static final String TAG = "RealtimeDB";
        private FirebaseDatabase database;
        private DatabaseReference unverifiedRef, companiesRef;

        String emailKey, ssnKey, phoneKey, companyId;

        int num;
        private FirebaseMethods firebaseMethods;
        private Context mContext;

        ArrayList<EmployeeUser> myListItems;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_register_addemployees);

            mContext = AddEmployeeActivity.this;

            firstNameET = (EditText) findViewById(R.id.firstNameET);
            lastNameET = (EditText) findViewById(R.id.lastNameET);
            phoneNumberET = (EditText) findViewById(R.id.phoneNumberET);
            emailET = (EditText) findViewById(R.id.emailET);
            ssnET = (EditText) findViewById(R.id.ssnET);

            checkmarkImage = (ImageView) findViewById(R.id.checkmarkImage);

            database = FirebaseDatabase.getInstance();
            unverifiedRef = database.getReference("/Unverified Employees");
            companiesRef = database.getReference("/Companies");
            currentUser =
                    FirebaseAuth.getInstance().getCurrentUser();

            checkmarkImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    final String email = emailET.getText().toString().trim();
                    final String ssn = ssnET.getText().toString().trim();
                    final String phone = phoneNumberET.getText().toString().trim();

                    //Add EditText info to Firebase UnverifiedEmployees Node
                    addDataToFirebase();               

                }
            });



        } //End of ONCREATE


        public void addDataToFirebase() {

            Log.i("BEFORE", "BEFORE COMPANIES REF");
            //companiesRef.addValueEventListener(new ValueEventListener(){
            companiesRef.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Log.i("COMPANIESREF", "INSIDE COMPANIES REF ONDATACHANGE");

                    EmployeeUser user = new EmployeeUser();

                    for (DataSnapshot ds : dataSnapshot.getChildren()) {
                        for (DataSnapshot ds2 : ds.getChildren()) {
                            for (DataSnapshot ds3 : ds2.getChildren()) {
                                for (DataSnapshot ds4 : ds3.getChildren()) {
                                    for (DataSnapshot ds5 : ds4.getChildren()) {


                                        Log.i(TAG, "checkIfUsernameExists: datasnapshot: " + ds5);

                                        //user.setCompanyId(ds5.getValue(EmployeeUser.class).getCompanyId());
                                        user.setCompanyId(ds5.getValue(String.class));

                                        Log.i(TAG, "checkIfUsernameExists: ID: " + user.getCompanyId());
                                        //callback.gotDataSnapshot(dataSnapshot);

    if (user.getCompanyId() != null) {
                                            companyId = user.getCompanyId();
                                        }
                                    }
                                }
                            }

                        }
                    }


                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Log.i("Cancelled", "In onCancelled: " + databaseError);

                }
            //};
            }); //END OF ADDLISTENER

           // companiesRef.addListenerForSingleValueEvent(eventListener);


            emailKey = unverifiedRef.child(currentUser.getUid()).push().getKey();
            unverifiedRef.child(currentUser.getUid()).child(emailKey).child("emailAddress").setValue(emailET.getText().toString(), completionListener);
            Log.i("getKey EMAIL", emailKey);

            ssnKey = unverifiedRef.child(currentUser.getUid()).push().getKey();
            unverifiedRef.child(currentUser.getUid()).child(ssnKey).child("socialSecurityNumber").setValue(ssnET.getText().toString(), completionListener);
            unverifiedRef.child(currentUser.getUid()).child(ssnKey).child("CompanyID").setValue(companyId, completionListener);
            Log.i("getKey SSN", ssnKey);
            Log.i("get COMPANY ID", companyId);

            phoneKey = unverifiedRef.child(currentUser.getUid()).push().getKey();
            unverifiedRef.child(currentUser.getUid()).child(phoneKey).child("phoneNumber").setValue(phoneNumberET.getText().toString(), completionListener);
            Log.i("getKey Phone", phoneKey);

        }

1 个答案:

答案 0 :(得分:0)

你无法以你的方式实现这一目标。根据其中一个孩子的价值,您无法获得父母。换句话说,如果ID L4bs8bH5...下方存在RC9zI1...,则无法获得RewriteEngine On RewriteRule ^$ view/pages/login [L] RewriteRule (.*) view/pages/$1 [L] 的值。要解决这个问题,您需要考虑使用非规范化,这是Firebase的常见做法。这意味着您需要复制数据才能获得这些结果。为此,我建议您看一下本教程Denormalization is normal with the Firebase Database,以便更好地理解。

因此,在这种情况下,您通常应考虑扩充数据结构以允许反向查找。例如,在您的方案中,我在userId下添加公司列表。这样做,您就可以查询属于单个用户的所有公司(如果存在的话)。