简体   繁体   中英

Firebase - Best way to reorder entries in realtime Database

Am trying to build a Queue management app. And I use Firebase push() to add new entries into Firebase real-time database. And the order of insertion is my default set.

For a particular scenario - I want to push some elements down by 2 or 3 steps . Whats the best way to do it ? Do I need to use firebase functions? Or Will I need a separate server to handle this logic ?

Example : Say , I have a set of 10 push requests from different client apps who want to get into the queue . When the client doesn't arrive , i want to push them 5-7 places down in the queue.

Is there a good approach to handle these scenarios without a server

I was trying to put the logic on the client side but I guess it wouldn't help much if the client goes offline when using the phone.

Also, I don't have a queue number set as a child element , since the insertion order is being taken care by firebase . A queue# I guess would create conflicting entries if 2 + clients are trying to enter the queue simultaneously .Not sure if this is the right approach.

Unfortunately, you cannot change the order of the nodes in the Firebase Console . All the nodes are by default ordered by key. If you need to order your database elements by a specific criteria, just add another property in your item object that will help you build a query that retrieves the items in the order you want. Let's say you want to order your items by timestamp and you have a database structure that looks like this:

Firebase-root
   |
   --- users
        |
        --- uid
             |
             --- name: "John"
             |
             --- age: 22
             |
             --- creationDate: 1534424049

To order your users by the date of creation, you should use a query that looks like this:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("users").orderByChild("creationDate");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM