使用Swift 4和Firebase的集合视图分页错误

时间:2019-02-08 02:51:59

标签: firebase firebase-realtime-database swift4

我尝试创建一个家庭供稿,该供稿可从当前用户关注的用户那里提取帖子。目前工作了一半。它从用户的关注中拉出帖子。

我在代码中有2个似乎无法修复的错误:

刷新错误

当用户出于某种原因拉动刷新时,它会两次显示为帖子,而我希望它只是显示原始的前5个帖子。

分页错误

我不太确定如何在当前显示最后一则帖子后阻止该帖子的加载,它只会继续加载最后5篇帖子,而不是停止不再加载。

获取以下信息:

bc

分页功能:

fileprivate func fetchFollowingUserIds() {
    guard let uid = Auth.auth().currentUser?.uid else { return }
    Database.database().reference().child("user-following").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in

        guard let userIdsDictionary = snapshot.value as? [String: Any] else { return }

        userIdsDictionary.forEach({ (key, value) in
            Database.fetchUserWithUID(uid: key, completion: { (user) in
                self.fetchPostsWithUser(user: user)
            })
        })
    })
}

刷新功能

var startKey: String!
fileprivate func fetchPostsWithUser(user: User) {

    guard let uid = Auth.auth().currentUser?.uid else { return }

    let ref = Database.database().reference().child("posts").child(user.uid)

    if startKey == nil {

        ref.queryOrderedByKey().queryLimited(toFirst: 5).observe(DataEventType.value) { (snapshot) in

            guard var allObjects = snapshot.children.allObjects as? [DataSnapshot] else { return }

            allObjects.reverse()

            allObjects.forEach({ (snapshot) in

                guard let dictionary = snapshot.value as? [String: Any] else { return }

                var post = Post(user: user, dictionary: dictionary)

                let postId = snapshot.key
                post.id = snapshot.key

                self.startKey = postId
                print(self.startKey)

                self.posts.append(post)

                self.posts.sort(by: { (post1, post2) -> Bool in
                    return post1.creationDate.compare(post2.creationDate) == .orderedDescending
                })
                self.collectionView?.reloadData()
            })
        }

    } else {

        print("Loading more posts")

        guard let lastD = startKey else { return }

        let queryRef = ref.queryOrdered(byChild: "creation_date")
        let queryEndingRef = queryRef.queryEnding(atValue: lastD)
        let queryLimitedRef = queryEndingRef.queryLimited(toLast: 5)
        queryLimitedRef.observeSingleEvent(of: .value) { (snapshot) in

            guard var postArray = snapshot.children.allObjects as? [DataSnapshot] else { return }


            postArray.reverse()
            postArray.removeFirst()

            postArray.forEach({ (snapshot) in

                print(snapshot.key)

                guard let dictionary = snapshot.value as? [String: Any] else { return }

                var post = Post(user: user, dictionary: dictionary)

                let postId = snapshot.key
                post.id = snapshot.key

                self.posts.append(post)

                self.posts.sort(by: { (post1, post2) -> Bool in
                return post1.creationDate.compare(post2.creationDate) == .orderedDescending
                })
                DispatchQueue.main.async {
                    self.collectionView?.reloadData()
                }
            })

        }
    }
}

override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    let currentOffset = scrollView.contentOffset.y
    let maxOffset = scrollView.contentSize.height - scrollView.frame.size.height
    if maxOffset - currentOffset <= 40{
        fetchAllPosts()
    }
}

0 个答案:

没有答案