0

I have uploaded to database sample JSON and customize a value just to see the code get executed meaning to make sure my query get a match and return back the object. So here is my implementation.

Query personQuery = myRef.orderByChild("mCalculateLeftEyeSizeWidth")
                .startAt(mCalculateLeftEyeSizeWidth())
                .limitToFirst(1);
        personQuery.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                if(dataSnapshot.exists()){
                Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
                Person person = dataSnapshot.getValue(Person.class);
                Log.d(TAG,"Person name:" + person.getName());
                }
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                if(dataSnapshot.exists()) {
                    Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey());
                    Person person = dataSnapshot.getValue(Person.class);
                    Log.d(TAG, "Person name:" + person.getName());
                }
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists()) {
                    Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey());
                }
            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {
                if(dataSnapshot.exists()) {
                    Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey());
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.d(TAG, "DatabaseError:" + databaseError.getMessage());
            }
        });

If I am running debug After Analyze image I see that the query do all the stuff here:

Query personQuery = myRef.orderByChild("mCalculateLeftEyeSizeWidth")
                    .startAt(mCalculateLeftEyeSizeWidth())
                    .limitToFirst(1);

Get the calculation correctly, I am looking at the debug personQuery and everything seems to be fine. but when I moving to the next part:personQuery.addChildEventListener

It jumps over it and my entire function executed is ended. What should I expect here? Does it now listen to every change? Does it mean it did not find a match even if I made sure manually that the same value exist? Do I need to actually upload another JSON file to trigger onChildAdded or onChildChanged?

My logic is the Permitted user take a photo. my app does face recognition, do math calculation, then query to the Firebase database to find a match according to value and suppose to return the person object back to the Android device for presentation. The mobile device only doing a query and return match, it does not update the Database with new records, this I do manually. So I am not sure if the "Listen" part gets a trigger or not.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • try using personQuery.addValueEventListener(new ValueEventListener() – karthik Jul 12 '17 at 12:48
  • Adding debug got me this: Listen at /People failed: DatabaseError: Permission denied. These are my { "rules": { "users": { "$uid": { ".read": "auth.uid === $uid", ".write": "auth.uid === $uid" } }, "People": { ".indexOn": [ "mCalculateLeftEyeSizeWidth", "mCalculateLeftEyeSizeHeight" ] } } } –  Jul 12 '17 at 12:58
  • Data is read from the Firebase Database asynchronously. Instead of waiting for the data to return (which would trigger an "Application Not Responding" dialog in Android), your regular code continues. Then once the data is available, your callback methods are triggered and the logcat calls prints, as you see in the output – Frank van Puffelen Jul 12 '17 at 13:26
  • The default security rules only allow authenticated users to access the data. For development you may consider relaxing those rules to allow public access, as shown here: https://stackoverflow.com/questions/37477644/firebase-permission-denied-error – Frank van Puffelen Jul 12 '17 at 13:26
  • Thanks Frank already did that. Eran –  Jul 12 '17 at 13:47

0 Answers0