0

My realtime-database is structured like this:

   - Procedure001
        - Post0001
            - user_id: 00001
            - timestamp: 1559214445

I would like to read the database and get only the posts that user0001 posted, and also sort them by most recent post (by timestamp).

Currently I am able to get only the posts by a certain user like so -

let ref = Database.database().reference().child("Procedure001").queryOrdered(byChild: "user_id").queryEqual(toValue: "0001")

How can I also sort the by timestamp using firebase?

Bhavik Modi
  • 1,517
  • 14
  • 29
adam eliezerov
  • 2,185
  • 2
  • 11
  • 17
  • @JoakimDanielson That's not Swift, also I want to sort and filter. In that post they only sort. – adam eliezerov May 30 '19 at 12:17
  • Not exactly. Because then it gives me all the posts. It does sort them but does not filter based on what user posted. – adam eliezerov May 30 '19 at 12:24
  • Firebase Database queries can only order/filter on a single property. In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. For an example of this and other approaches, see my answer here: http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase. Client-side ordering/filtering on the second property is one of the options mentioned there. – Frank van Puffelen May 30 '19 at 13:26

2 Answers2

0

I ended up getting the posts and filtering them so I only get posts by a specific user using firebase, and sorting them according to timestamp afterwards using the new Swift 5 sort() function - I used the swift 5 answer on this thread Swift how to sort array of custom objects by property value

adam eliezerov
  • 2,185
  • 2
  • 11
  • 17
0

I realize you posted an answer and while it will work, there are some limitations.

If the user posts a million posts, trying to load all of those in and sort in code may overwhelm the device. even if it's a smaller amount of nodes, it's much faster to have Firebase do the sort and present the data than having your app process through it all.

So often times you will find the solution is to duplicate the data depending on the types of queries you want to run. In this case you have a node that stores the user id and time stamp of each post

Procedure
   post_0
      uid: uid_0
      timestamp: 20190529
   post 1
      uid: uid_0
      timestamps: 20190530

As you mentioned you can query for all posts from uid_0 and sort by timestamp in code. But, check this out...

user_posts
   uid_0
      post_0: 20190529
      post_1: 20190530

By adding an additional node that keeps track of the user posts the query can be eliminated since all of the posts are stored in one place, and can be easily sorted on the server.

I'll throw one other option out, compound values.

Procedure
   post_0:
      uid_timestamp: uid_0_20190529
   post_1:
      uid_timestamp: uid_0_20190530

Using this structure, you can query for all uid_0's posts, ordered by uid_timestamp.

Jay
  • 34,438
  • 18
  • 52
  • 81