0

I'd like to create an application that includes something like an rss feed or a feed that you might see instagram or facebook. I'm currently trying to accomplish this using the firebase real time database or firestore as a backend to store the posts shown to the users. I can't seem to think of a way to make this work. The ideal solution sends a list of posts in chronological order that doesn't need any additional sorting on the client. When I tried to use the real time database, I could easily add all the relevant posts in their own path and sort them on the client side (or maybe use cloud functions to sort the data on the server side). When trying to come up with the solution using firestore, my idea was to have the post document hold a reference to a sub collection that holds the list of subscribers/followers but I can't seem to find a way to select a post based on whether a document exists in a referenced sub collection.

FYI, the reason why I want the ideal solution is because PAGINATION.

  • Is **[this](https://stackoverflow.com/questions/51326473/how-to-query-a-firestore-collection-using-a-property-from-a-step-deeper-with-kot/51326800)** what you are looking for? – Alex Mamo Jul 15 '18 at 12:43
  • 1
    Regarding pagination, **[this](https://stackoverflow.com/questions/50741958/how-to-paginate-firestore-with-android)** is a recommended way in which you can paginate queries by combining query cursors with the limit() method. I also recommend you take a look at this **[video](https://www.youtube.com/watch?v=KdgKvLll07s)** for a better understanding. – Alex Mamo Jul 15 '18 at 12:44
  • @AlexMamo thanks for the additional links, especially the information regarding pagination. – Joel Robinson-Johnson Jul 15 '18 at 23:49

1 Answers1

0

I have little to no experience with Firestore. With Firebase however, if I understand you correctly, this is easy to achieve.

The first part is storing your date, make sure you store it as a timestamp (date.getTime()).

Then you can do the following:

db.ref('posts').orderByChild('date').limitToLast(10)

This allows you to fetch the last 10 posts. To get the previous 10 you grab the date from the earliest of those posts and do:

db.ref('posts').orderByChild('date').endAt(previousDate - 1).limitToLast(10)

Note that you might want to handle this differently if 2 posts have the same date value.

EECOLOR
  • 11,184
  • 3
  • 41
  • 75
  • if this is done with the real time database, posts are returned in the order in which they were added to the path, not when the posts where created. If you subscribe to a new user, all the post would be appended to the end of the list, not in chronological order. If using firestore, I would put all the posts in a single collection (listed in chron order) and then return the post if the user subscribes to the author. Due to the size constraints a document can have I thought it might be best to save the list of followers as a referenced sub collection. When it comes to firestore I'm trying to... – Joel Robinson-Johnson Jul 13 '18 at 21:27
  • limit denormalization because firestore querying is depending on the number of objects returned. – Joel Robinson-Johnson Jul 13 '18 at 21:28
  • scratch that....lapse in judgement ...I think you may have something... I am trying to limit denormalization..but your way may be viable, i'll try it out. – Joel Robinson-Johnson Jul 13 '18 at 21:29