0

I want to query that orderByValue it's always return null But when i remove .orderByValue data load normally. Please help

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
    mDatabase.child("newfeed").child(FirebaseRef.mAuth.getCurrentUser().getUid()).orderByValue().limitToFirst(10).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){
                for (DataSnapshot data : dataSnapshot.getChildren()) {
                    lastRating = data.getValue(Double.class);
                    loadData(data.getKey());
                    Log.d("Key ", data.getKey());
                }
            }
            else{
                mProgressBar.setVisibility(RecyclerView.GONE);
                mProgressBarTop.setVisibility(RecyclerView.GONE);

            }
        }
        @Override public void onCancelled(DatabaseError databaseError) {
            mProgressBar.setVisibility(RecyclerView.GONE);
        }
    });
}

Here is example of my JSON.

"newfeed" : {
"7gE2qiqoblSHDHubiEW0IzrhzIQ2" : {
  "-Kn090eIxMH338xIwA8J" : 69259.23018812927,
  "-Kn092jf25h2Qdmk--RX" : 69268.71984827428,
  "-Kn093LPOBCCE1-Ygx6t" : 69270.82899176712,
  "-Kn093P2PE2rIml4XyKI" : 121518.05765189118,
  "-Kn09lD0cGUhRQPbvJtS" : 69394.93008559482,
  "-Kn0BYI-7IQzkCmBH8gN" : 69892.8262113143,
  "-Kn4UkNCe1rhpiJ_Rrew" : 3133576.6418433865
}
JustKhit
  • 407
  • 3
  • 9
  • You've included a picture of the JSON tree in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export JSON link in [your Firebase Database console](https://console.firebase.google.com/project/_/database/data/). Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. – Frank van Puffelen Jun 20 '17 at 13:42
  • @FrankvanPuffelen Sorry for that, Just added. – JustKhit Jun 20 '17 at 17:27
  • Does it make any difference if you use `addValueEventListener` ? See https://stackoverflow.com/questions/34486417/firebase-offline-capabilities-and-addlistenerforsinglevalueevent for some subtelties around how `addListenerForSingleValueEvent` behaves (though not certain why this would only be case when `orderByValue()` is used). As that post describes, you probably need to call `keepSynced(true)` when using `addListenerForSingleValueEvent` – John O'Reilly Jun 20 '17 at 18:36

1 Answers1

0

I'm not really sure what you're getting, which is why I ran this code myself:

ref.orderByValue().limitToFirst(10).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot data : dataSnapshot.getChildren()) {
            Log.d("Key ", data.getKey()+": "+data.getValue());
        }
    }
    @Override public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
});

It prints:

D/Key: -Kn090eIxMH338xIwA8J: 69259.23018812927

D/Key: -Kn092jf25h2Qdmk--RX: 69268.71984827428

D/Key: -Kn093LPOBCCE1-Ygx6t: 69270.82899176712

D/Key: -Kn09lD0cGUhRQPbvJtS: 69394.93008559482

D/Key: -Kn0BYI-7IQzkCmBH8gN: 69892.8262113143

D/Key: -Kn093P2PE2rIml4XyKI: 121518.05765189118

D/Key: -Kn4UkNCe1rhpiJ_Rrew: 3133576.6418433865

From a quick scan that looks like the correct order to me.

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807