【问题标题】:Loading next 15 posts each time the bottom of the page is reached每次到达页面底部时加载接下来的 15 个帖子
【发布时间】:2017-08-12 00:14:10
【问题描述】:

我有一个函数可以从我的 Firebase 数据库中获取帖子并填充集合视图,一次 15 个帖子使用:

ref.child("posts").queryOrderedByKey().queryLimited(toLast: 15).observeSingleEvent(of: .value, with: { (snap) in

现在我希望每次用户到达屏幕底部时获取接下来的 15 个帖子。在我的cellForItem 方法中,我试图检查是否已到达页面底部,如果已到达,则在其停止的位置加载接下来的 15 个帖子。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "postCell", for: indexPath) as! PostCell

    cell.postImage.loadImageUsingCacheWithUrlString(posts[indexPath.row].pathToImage)
    cell.postID = posts[indexPath.row].postID
    cell.postImage.contentMode = UIViewContentMode.scaleAspectFill

    if indexPath.row == posts.count - 1
    {
        fetchPosts()
    }

    return cell
}

我只是不知道如何指定在加载接下来的 15 个帖子时,它们需要从最后 15 个停止的地方开始,而不是再次获取数据库中的所有帖子。

这是整个fetchPosts()函数:

func fetchPosts() {

    let ref = FIRDatabase.database().reference()
    ref.child("users").queryOrderedByKey().observe(.value, with: { snapshot in

        let users = snapshot.value as! [String : AnyObject]

        for (_, value) in users {

            if let uid = value["uid"] as? String {

                if uid == FIRAuth.auth()?.currentUser?.uid {

                    if let followingUsers = value["following"] as? [String : String] {

                        for (_, user) in followingUsers {
                            self.following.append(user)
                        }
                    }
                    self.following.append(FIRAuth.auth()!.currentUser!.uid)

                    ref.child("posts").queryOrderedByKey().queryLimited(toLast: 15).observeSingleEvent(of: .value, with: { (snap) in

                        for postSnapshot in snap.children.allObjects as! [FIRDataSnapshot] {
                            let value = postSnapshot.value as! [String : AnyObject]

                            if let userID = value["userID"] as? String {
                                for each in self.following {
                                    if each == userID {

                                        let posst = Post()
                                        if let poster = value["poster"] as? String, let likes = value["likes"] as? Int, let pathToImage = value["pathToImage"] as? String, let postID = value["postID"] as? String {

                                            posst.poster = poster
                                            posst.likes = likes
                                            posst.pathToImage = pathToImage
                                            posst.postID = postID
                                            posst.userID = userID
                                            if let people = value["peopleWhoLike"] as? [String : AnyObject] {
                                                for (_, person) in people {
                                                    posst.peopleWhoLike.append(person as! String)
                                                }
                                            }
                                            posts.append(posst)
                                        }
                                    }
                                }
                                self.collectionView.reloadData()
                            }
                        }
                    })
                    ref.removeAllObservers()
                }
            }
        }
    })
}

我可以在此处添加/更改什么以确保每次到达页面底部时按顺序加载 15 个帖子?

【问题讨论】:

标签: ios swift firebase firebase-realtime-database uicollectionview


【解决方案1】:

没有尝试过,但这是一个想法。 我会设置两个变量:

let readLimit:Int = 15
var readOffset:Int = 15

第一个变量是硬编码的,代表限制,应该加载新结果。

在cellForItem函数中,检查:

// Check if user scrolled to last row
if (indexPath.item + 1) == readOffset {
  // Yes, scrolled to last row
  // Increase limit to load more from database
  readOffset += readLimit

  // Call function which loads data from database
  self.fetchPosts()
} 

你知道我在哪里吗?现在您必须稍微修改一下读取功能: 更改queryLimited(toLast: 15):将15替换为self.readOffset

记住这里没有经过测试,只是写在这里帮助大家理解。

【讨论】:

  • 尝试这个,看起来它没有遵循 15 的 readLimit,因为在加载应用程序时,集合视图会无限填充,并且有很多重复项。
猜你喜欢
  • 1970-01-01
  • 2018-05-22
  • 2012-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多