0

I have no issue sorting my database in descending order by using orderbychild(-timestamp) in firebaseRecycleradapter.

 final FirebaseRecyclerAdapter<UserChat, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<UserChat, BlogViewHolder>(
            UserChat.class,
            R.layout.userchatrow,
            BlogViewHolder.class,
            mUserChatList.child(mAuth.getCurrentUser().getUid()).child("UserList").orderByChild("negatedtime")
           //descending order
    ) {

However, when user is logged out using Auth.getinstance.signOut and LoginManager.getinstance.logOut, then log in again (either facebook, google or email), firebase database can no longer fetching latest result via orderbychild(-timestamp). When I deleted orderbychild(-timestamp), it can retrieve the latest result, but in ascending order which is not I want.

final FirebaseRecyclerAdapter<UserChat, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<UserChat, BlogViewHolder>(
            UserChat.class,
            R.layout.userchatrow,
            BlogViewHolder.class,
            mUserChatList.child(mAuth.getCurrentUser().getUid()).child("UserList")
             //ascending order
    ) {

I can't reverse the LayoutManager nor RecyclerView, or the other approaches such as reversing the ArrayList order as suggested here as the key is not a push ID, the only way to sort to the latest is by orderbychild(-timestamp).

I did not use setPersistenceEnable and keepSynced, so as not to cache any memory on user phone, but to no avail.

Any helps?

Zhen Jing Heng
  • 109
  • 2
  • 8
  • Please add the code you using. – Alex Mamo Feb 12 '18 at 10:35
  • Are you using persistanceEnabled ?? Check – HarshitG Feb 12 '18 at 10:39
  • I did not use `FirebaseDatabase.getInstance().setPersistenceEnabled(true or false);`. Is it being set to true by default? – Zhen Jing Heng Feb 12 '18 at 10:49
  • It defaults to `false`. Leave it as is, it shouldn't affect your query results. – Frank van Puffelen Feb 12 '18 at 15:16
  • You say "firebase database can no longer fetching latest result via `orderbychild(-timestamp)`". What goes wrong when you use `mUserChatList.child(mAuth.getCurrentUser().getUid()).child("UserList").orderByChild("negatedtime")` after the user logged out and back in again? It seems extremely unlikely that the same code returns the children in a different order then. – Frank van Puffelen Feb 12 '18 at 15:18
  • When user logged out and back in again, if there is a latest node added in there, it can't retrieve the latest result and sort it descending from there, it still sorts it from the last negatedtime (last previous node). – Zhen Jing Heng Feb 13 '18 at 09:40
  • However, I managed to solve them ridiculously, and I not sure how and why.. The way I solved it was placing a `mUserChatList.child(mAuth.getCurrentUser().getUid()).child("UserList").addListenerForSingleValueEvent()` without `orderByChild("negatedtime")` before the firebaserecyclerAdapter, and other code remains the same. – Zhen Jing Heng Feb 13 '18 at 09:54

0 Answers0