FirebaseUI-Android icon indicating copy to clipboard operation
FirebaseUI-Android copied to clipboard

Descending query when using orderByChild()

Open anrodon opened this issue 10 years ago • 11 comments

Hey there, @pautena and I are developing an Android app with a Firebase backend at Hack The North 2016.

We've faced this problem and we think that could be nice to solve with a new feature in future versions:

  • When using the Firebase Realtime Database we were trying to get a DatabaseReference like this mFirebaseDatabaseReference.child(User.USER_CHILD).orderByChild("points") to make a ranking, but we faced the problem that Firebase only retrieves the ordered data in ascendent mode.

We think that would be awesome to have the possibility to query orderByChild with some kind of parameter which indicates if you want your data ordered in an ascendent or descendent way.

We post this issue to stay tuned for improvements in this area and hope Firebase become even more cool! :D

anrodon avatar Sep 17 '16 23:09 anrodon

See #90, it should solve your issue.

SUPERCILEX avatar Sep 17 '16 23:09 SUPERCILEX

Yeah, @SUPERCILEX, we saw this issue like 30 minutes ago, it's effective, but I'm sure Firebase can give us a cleaner and smother way in the future.

Thank you anyway for the help :)

anrodon avatar Sep 18 '16 00:09 anrodon

We've found a good solution if you're using the DatabaseReference to populate a FirebaseRecyclerAdapter, here it goes:

  • Inside the FirebaseRecyclerAdapter declaration, override the getItem() function just like we did:
@Override
public User getItem(int position) {
        return super.getItem(getItemCount() - 1 - position);
}

Hope it helps anybody with the same problem :D

anrodon avatar Sep 18 '16 00:09 anrodon

Oooho.... that's a neat trick Andreu!

On Sat, Sep 17, 2016 at 5:28 PM Andreu Rodríguez i Donaire < [email protected]> wrote:

We've found a good solution if you're using the DatabaseReference to populate a FirebaseRecyclerAdapter, here it goes:

  • Inside the FirebaseRecyclerAdapter declaration, override the getItem() function just like we did:

@Override public User getItem(int position) { return super.getItem(getItemCount() - 1 - position); }

Hope it helps anybody with the same problem :D

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/firebase/FirebaseUI-Android/issues/310#issuecomment-247816246, or mute the thread https://github.com/notifications/unsubscribe-auth/AA3w3yP44_uY5yw2-O2AnNBFbzfkt3mjks5qrIW-gaJpZM4J_v_- .

puf avatar Sep 18 '16 02:09 puf

The problem with your original request is that it requires a change to the Firebase SDK. While that is a valid request (and I'll use it to remind our engineers of the demand for this feature), it is unrelated to FirebaseUI. I'm closing the issue here, but must admit that your workaround is embarrassingly simple. We should've thought of that one earlier. :-)

puf avatar Sep 18 '16 02:09 puf

This will work ok if you are returning the entire search query - I believe you hit problems when you limit the range that you are returning using limitToFirst(); In my case I'm trying to return the highest 100 scores based on a child, but if the query returns more than 100, then I get the lowest 100 ranked in the correct order.

To solve this, I had to store the negated (negative) score and use the ascending sort. This also requires that I comment out the following from the example code, otherwise the results are flipped (top 100, but lowest to highest).

// manager.setReverseLayout(true); // manager.setStackFromEnd(true);

Hope this helps.

plthatfunkymusic avatar Nov 08 '16 09:11 plthatfunkymusic

The problem with @anrodon's solutions is that now the list is not updated automatically when new data is added. However, It works when I do the query again.

Shajeel-Afzal avatar Sep 29 '17 10:09 Shajeel-Afzal

@Shajeel-Afzal it's true.it's not work when new data updated to the database. But,Great work dude...

Suhailakl avatar Apr 08 '18 13:04 Suhailakl

I Found a solution to solve the issue on firebase recycler view as reported by @Shajeel-Afzal . After doing the solution of @anrodon just override another firebase recycler method onDataChange as below, @Override public void onDataChanged() { recycle.removeAllViews(); super.onDataChanged(); } now you will get real time updation of items in recycler view.

Suhailakl avatar Apr 08 '18 14:04 Suhailakl

Hi Suhailakl, could you better explain where this code should be included? I can not make it work. Thank you.

Raulsc9 avatar Jul 28 '18 19:07 Raulsc9

Hi Suhailakl, could you better explain where this code should be included? I can not make it work. Thank you.

You can add it to onCreate

Niraj-Ranjan avatar Jun 13 '21 17:06 Niraj-Ranjan