-1

I would like to retrieve the posts from the following users by their IDs. I have reviewed many articles, but I haven't found a solution yet. Maybe, I am not looking at the right place.

Database Structure:

 Firestore-root
   |
   --- users (collection)
   |     |
   |     --- uid (documents)
   |          |
   |          --- name: "User Name"
   |          |
   |          --- email: "email@email.com"
   |
   --- following (collection)
   |      |
   |      --- uid (document)
   |           |
   |           --- userFollowing (collection)
   |                 |
   |                 --- uid (documents)
   |                 |
   |                 --- uid (documents)
   |
   --- posts (collection)
         |
         --- uid (documents)
              |
              --- userPosts (collection)
                    |
                    --- postId (documents)
                    |     |
                    |     --- title: "Post Title"
                    |     |
                    |     --- date: September 03, 2018 at 6:16:58 PM UTC+3
                    |
                    --- postId (documents)
                          |
                          --- title: "Post Title"
                          |
                          --- date: September 03, 2018 at 6:16:58 PM UTC+3

Function to retrieve posts:

static func subscribe(userID: String) -> AppThunkAction {
        AppThunkAction { dispatch, _ in

            let listener = Snapshot<Model.Photo>.listen(.photos(userID: userID), queryBuilderBlock: {
                $0.order(by: .updateTime, descending: true).limit(to: 30)
            }) { result in
                switch result {
                case let .success(photos):
                    dispatch(PhotosAction.updatePhotos(photos: photos))
                case let .failure(error):
                    print(error)
                    // error handling
                    dispatch(PhotosAction.updatePhotos(photos: []))
                }
            }
            dispatch(PhotosAction.updateListener(listener: listener))
        }
    }
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
Peace Cloud
  • 43
  • 1
  • 6

1 Answers1

0

There are some ways that you can achieve that. You will need to get the uid per users and compare it with the uid from the posts. I would recommend you to take a look at the Community post Firestore: Queries and Security (Swift). This post provide some ideas on how to achieve that.

Besides that, the official documentation Querying and filtering data provides examples of queries with Swift.

These other two below posts should provide you some additional insights on querying with Swift, per ids.

I could find these below two repositories, that have Firestore post applications - not in Swift - that might help you get ideas as well, on how to perform queries and the logic to retrieve posts per user.

Let me know if the information helped you!

gso_gabriel
  • 4,199
  • 1
  • 10
  • 22
  • Not exactly :(. Maybe, I am not good enough to understand. The point that I don't understand. I can get all IDs of users that I follow. But, I don't know how to query posts by these IDs at once. By the abovementioned query in my post, I can only query one user. – Peace Cloud Mar 06 '20 at 11:04
  • Hi @PeaceCloud There are some documentation on it - as this one [here](https://firebase.google.com/docs/firestore/query-data/get-data#get_multiple_documents_from_a_collection) and [here](https://medium.com/google-developers/update-and-customize-queries-on-your-cloud-firestore-data-for-actions-on-google-7eb1010b417f) - that you can check and this very good post on the [Community](https://stackoverflow.com/questions/46721517/google-firestore-how-to-get-document-by-multiple-ids-in-one-round-trip), that provides more insights on it. You should understand better queries of multiple documents. – gso_gabriel Mar 06 '20 at 11:24