1
votes

I'm building social network application with Firebase and Swift. I have user who have some followings to another users. I want to parse a feed for this user that contains posts only from his list of followings. My database looks like this:

    "posts": {
       "postA": {
          "title": "Title for post A"
          "byUser": "userABC"
          }
       }
       "postB": {
          "title": "Title for post B"
          "byUser": "userCBA"
          }
       }
       "postC": {
          "title": "Title for post C"
          "byUser": "userBCA"
          }
       }
       "postD": {
          "title": "Title for post D"
          "byUser": "userBCD"
          }
       }
   }
   "users": {
      "userABC": {
         "name": "John"
         "following": {
            "userCBA": "true"
            "userBCA": "true"
         }
      }
      "userCBA": {...}
      "userBCA": {...}
      "userBCD": {...}
   }

So the idea is to show filtered posts for userABC for his list of followings (postB & postC). What Firebase request I should make?

P.S.: the only idea I have is to make several firebase requests for each user to download their posts, then to append those into an array and then order array objects by timestamp. But imagine thousand of followings and each have a thousand of posts.. It will be a disaster.

P.P.S.: I can't fiter result by Firebase "Security & Rules" because I want to show all posts by all users in search.

Thanks a lot!

1

1 Answers

3
votes

In NoSQL you often model the data for the way your application wants to use it. Since you want to show the user a feed/wall of the posts of people they follow, model that in your database:

"user_walls": {
  "userABC": {
     "KeyOfPost1FromUserCDA": true
     "KeyOfPost2FromUserBCA": true
     "KeyOfPost3FromUserCDA": true
  },
  "userBCA": {
     "KeyOfPost1FromUserCDA": true
     "KeyOfPost3FromUserCDA": true
     "KeyOfPost4FromUserABC": true
  },
  etc
}

Here we just store the keys of the posts, but you can also store more information instead of just true. This is a process known as denormalization.

For this and more relevant advice, I recommend this article on NoSQL Data Modeling.

For a great example application that models its data this way, see FireFeed: https://firefeed.io/.