0

I'm creating an instagram style feed and trying to figure out how to paginate posts based on people that you follow.

My data is structured as follows:

 posts:
      user1:
           post1
           post2
      user2:
           post3
           post4
      user3: 
           post5
           post6
           post7

 user-following:
      user1:
           user2
           user3

So posts are organized by the user that created the post. I also have a structure for people that the current user follows. So i grab the uid's of the users that are followed by the current user, then go to the post section and get all of the posts from those users, and each post has a createdAt value.

When i use queryByChild("createdAt"), it sorts the data per user, not absolutely. So it sorts user2's posts, then user3's posts, instead of doing an absolute sort since its pulling the data in chunks.

I need the most recent overall posts independent of the users that own the posts for the news feed. i can get all the post info in a snapshot, but is there a way to then sort that snapshot? I need to find a way to get the database to sort the information as a whole, not in chunks, then pull the most recent 'x' amount of posts from that sorted list.

Otherwise i have to pull everything into my program, then sort it in an array, then pull the most recent 'x' amount of posts from the array for pagination. problem is if there are 1000 posts, that's a massive data pull that would make the app super slow and possibly crash. any suggestions?

Dirty
  • 35
  • 5
  • The Firebase documentation is very helpful. Check out [Work with Lists of Data on iOS](https://firebase.google.com/docs/database/ios/lists-of-data) which states *queryLimitedToLast: Sets the maximum number of items to return from the end of the ordered list of results.* In this case, set this to 15, which would return the 15 'most recent' posts. In the future, please include the problematic code in your question along with your Firebase structure (as TEXT). You may want to review [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Jay May 31 '17 at 17:52
  • @Dirty If you want Jay to look at your reply, use "@Jay" in comments – Vlad Pulichev Jun 01 '17 at 05:43
  • @Jay Thank you for the reply. My problem goes a little deeper, I have to get the posts of people that the user follows. so the query gets each users posts in a chunk, then goes to the next user, etc. so it doesn't sort the posts once all of the data is observed, but in chunks instead. So the posts do not get sorted absolutely, but only by each individual user. Is there a way to simply grab the data all at once in a snapshot, then sort it, then pull the last 15? – Dirty Jun 01 '17 at 15:54
  • Sure. But that could be a LOT of data and exceed the capacity of the device and also cause slow UI. Looking at your structure, you could simply add observers to each of the posts/user that each user is interested in. i.e. user1 would add observers on user2 and user 3. So then, as posts are added they will receive the posts in an event. Only get the last 15 for each one. However, that may not be what you want so adding code to your question would help us understand this use case. – Jay Jun 01 '17 at 16:01
  • @Jay, I'll try and post some code here in a little, but yeah that doesn't solve the problem. The posts would still be getting sorted from user to user, instead of getting sorted absolutely. I would need a way of looking at all the posts from people that a user follows, then sorting it in the database, then pulling the last 'x' amount of posts from the database. Feel like this is a big issue with a NoSQL backend. In a SQL system, i feel like you could do a join of people followed and all of their posts, then sort, then pull data. – Dirty Jun 01 '17 at 16:22

1 Answers1

1

In my opinion the only one way here is:

1) Go through each user, that you follow

2) Get last X posts

3) Sort results array by date

4) Present last X of results array.

If you have table view:

To make an infinite scrolling you need to store this array and get posts from it.

On refresh - reload array of posts.

I have same structure and I think it is only way, how we can do it.

Hope it is correct and helps

Vlad Pulichev
  • 3,162
  • 2
  • 20
  • 34
  • Thank you for the reply. It still does not however solve the problem. Getting the last 'x' posts from each user does not satisfy the requirements of grabbing the most recent posts out of all users that you follow. Say you follow 500 people, you can't go through and grab the last 'x' posts from every user, because there is no way of determining which posts are the most recent of the 500 users. Is pagination with this structure not doable in firebase? – Dirty Jun 05 '17 at 06:14
  • @Dirty how do u generate id's for nodes of posts? I have same structure, I'm using childByAutoId. I have done infinite scrolling in my table view. With this alghorithm – Vlad Pulichev Jun 05 '17 at 06:17
  • I also use childByAutoId. So did you solve this issue? I'm not sure what infinite scrolling is, but if you figured this out please let me know how to do it! – Dirty Jun 15 '17 at 07:41
  • @Dirty Infinite scrolling - download X count of posts, scroll to the end, download next X posts in table view and again and again. I'm doing it in the way, which I have wrote in answer. – Vlad Pulichev Jun 15 '17 at 07:43
  • so you pull the last X amount of posts from each user and store it into an array, and then pull the last Y amount of posts from that array, and when the bottom of the scroll view is reached you pull in another Y amount of posts from the array correct? I was thinking thats the only way to do it. Just worried that the initial data pull would take a while if the amount of people you follow is very large – Dirty Jun 15 '17 at 07:51
  • @Dirty Yeah, I'm doing it this way. I will remember this question. And if i find better solution, I will comment here. Trust me:) It's really interesting question, how to do it with better perfomance. But it is only one way, that I have founded – Vlad Pulichev Jun 15 '17 at 07:53
  • thanks a lot man! I accepted your answer. If i find anything better i'll post it here as well and let you know. – Dirty Jun 15 '17 at 07:55
  • @Dirty Thanks. I have already favourited it:) So, I will have notification anyway – Vlad Pulichev Jun 15 '17 at 07:56
  • @Dirty any new suggestions?:) – Vlad Pulichev Jul 14 '17 at 05:48
  • @Dirty https://firebase.googleblog.com/2015/10/client-side-fan-out-for-data-consistency_73.html – Vlad Pulichev Jul 14 '17 at 07:53
  • 1
    https://stackoverflow.com/questions/45100384/firebase-fun-out-structure-for-posts#45100536 – Vlad Pulichev Jul 14 '17 at 10:30