Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何获取当前登录用户正在跟踪的用户的帖子_Ios_Swift_Firebase_Google Cloud Firestore - Fatal编程技术网

Ios 如何获取当前登录用户正在跟踪的用户的帖子

Ios 如何获取当前登录用户正在跟踪的用户的帖子,ios,swift,firebase,google-cloud-firestore,Ios,Swift,Firebase,Google Cloud Firestore,我正在使用Cloud Firestore为我的应用程序存储数据。我在从当前登录用户遵循的用户获取帖子时遇到问题。我已经通过这样的数据库进行了结构化 Firestore-root | --- users (collection) | | | --- uid (documents) | | | --- name: "User Name" | | | --- emai

我正在使用Cloud Firestore为我的应用程序存储数据。我在从当前登录用户遵循的用户获取帖子时遇到问题。我已经通过这样的数据库进行了结构化

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)
         |
         ------- postId (document)
                    |
                    |
                    --- uid: user
                    |
                    --- timestamp: 1570044062.261539
                    |
                    --- status: "status"
当我为提要取帖子时。我将查询返回的数据拆分为批,并按时间戳对批进行排序。每个批次有10篇文章,但是这些批次不包含当前登录用户遵循的用户的文章。它读取当前存储在数据库中的所有帖子,而不管谁在跟踪谁

postsQuery = db.collection("posts").order(by: "timestamp", descending: true).limit(to: 10)
当用户登录并加载提要时,fetchFirstBatch()函数会发送前10篇文章的请求

一旦用户滚动到包含所有帖子的表视图的底部,下一批帖子将由fetchNextBatch()函数获取


如何附加当前登录用户正在跟踪的用户的所有帖子,同时对每批获取的数据进行分页?我试图复制的提要结构是Instagram提要。

最好将您的问题分解为更小的部分,因为您提出的问题是您的应用程序及其实现的全部功能

我不明白为什么要将下面的集合从用户集合中分离出来,并将它们放在数据模型的顶层,因为基本上在那里,您已经创建了另一个层来将userFollowing子集合添加到用户文档中。您可以将userFollowing重新定位到下一级别的用户文档。同样,这取决于您是要存储用户(用户跟踪的人)的全部数据,还是用他们自己的uid填充他们的文档

但是查看数据模型的当前状态类似于下面的代码可以帮助您:

const arrayOfPeopleWhichUserFollows = await db.collection('following').doc(userId).collection('userFollowing').get()
      .then(querySnapshot => {
        return  querySnapshot.docs.map((doc) => {
          return doc.data().uid;
        });
      });


    // you have to measure the size of arrayOfPeopleWhichUserFollows to check whether it exceeds the limitation of 10
    // for using in the "where in" query from firestore
    // then breaks the array into a smaller piece, as small as 10 items per array and the repeat the below part for all the
    // fragmented arrays and append all the results into a single variable

    const firstPosts = await db.collection('posts')
      .where('uid', 'in', fragmentedArrayOfPeopleWhichUserFollows)
      .orderBy('timestamp', 'desc').limit(10);

    const posts = await firstPosts.get()
      .then(querySnapshot => {
        const lastVisible = querySnapshot.docs[querySnapshot.docs.length - 1];
        return querySnapshot.docs.map((post) => {
          return post.data();
        });
        const nextPosts = db.collection('posts')
          .where('uid', 'in', fragmentedArrayOfPeopleWhichUserFollows)
          .orderBy('timestamp', 'desc')
          .startAfter(lastVisible)
          .limit(10)
      });
考虑阅读以下链接:


感谢您的回复。因此,我获取当前登录用户跟踪的所有用户,并将UID存储在一个数组中。然后,当我检索帖子时,我必须有一个where子句,它根据该数组中的UID过滤掉帖子。我查看了where子句,由于某种原因,在使用swift编程时它不存在。我看了一下您提供的文档,他们有一个示例,但至少对于swift模块
postsQuery.whereField(“uid”,in:fragmentedarrayofppeoplewhichuserfollows)来说,它似乎是不推荐的
@jackrogers我一开始没有注意到您正在使用swift进行编码,所以我提供了JS代码,但您已经知道基本原理是一样的,我不知道何处提到where字段已被弃用,因为您也可以在firestore::的swift引用中找到它。似乎我没有将我的SDK更新为最新版本。它出现在6.11版上,6.12版中引入了
whereField(:in:)
查询。我已经更新了它,通过查看您的JS代码,逻辑是有意义的,并且是正确的答案。谢谢你的帮助
private func fetchNextBatch() {
        print("Fetching next batch")
        fetchingBatch = true

        postsQuery.start(afterDocument: lastSnapshot).getDocuments { (snapshot, error) in
            guard let snapshot = snapshot else {
                print("Error retrieving batch: \(error.debugDescription)")
                return
            }

            guard let lastSnapshot = snapshot.documents.last else {
                print("No more batches to fetch")
                self.fetchingBatch = false
                return
            }

            for document in snapshot.documents {
                let data = document.data()
                let postType = data["post type"] as? String ?? ""

                if postType == PostType.Status.type {
                    let status = data["status"] as? String ?? ""
                    let timestamp = data["timestamp"] as? Double ?? 0
                    let uid = data["user id"] as? String ?? ""
                    let username = data["username"] as? String ?? ""

                    self.posts.append(Post(status: status, timestamp: timestamp, postType: PostType.Status, userId: uid, username: username))
                }
                self.tableView.insertRows(at: [IndexPath(row: self.posts.count - 1, section: 0)], with: .automatic)
            }

            self.lastSnapshot = lastSnapshot

            DispatchQueue.main.async {
                self.tableView.reloadData()
            }

            self.fetchingBatch = false
        }
    }
const arrayOfPeopleWhichUserFollows = await db.collection('following').doc(userId).collection('userFollowing').get()
      .then(querySnapshot => {
        return  querySnapshot.docs.map((doc) => {
          return doc.data().uid;
        });
      });


    // you have to measure the size of arrayOfPeopleWhichUserFollows to check whether it exceeds the limitation of 10
    // for using in the "where in" query from firestore
    // then breaks the array into a smaller piece, as small as 10 items per array and the repeat the below part for all the
    // fragmented arrays and append all the results into a single variable

    const firstPosts = await db.collection('posts')
      .where('uid', 'in', fragmentedArrayOfPeopleWhichUserFollows)
      .orderBy('timestamp', 'desc').limit(10);

    const posts = await firstPosts.get()
      .then(querySnapshot => {
        const lastVisible = querySnapshot.docs[querySnapshot.docs.length - 1];
        return querySnapshot.docs.map((post) => {
          return post.data();
        });
        const nextPosts = db.collection('posts')
          .where('uid', 'in', fragmentedArrayOfPeopleWhichUserFollows)
          .orderBy('timestamp', 'desc')
          .startAfter(lastVisible)
          .limit(10)
      });