Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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
如何使用swift和firebase分页?_Swift_Firebase_Pagination_Swift4.1 - Fatal编程技术网

如何使用swift和firebase分页?

如何使用swift和firebase分页?,swift,firebase,pagination,swift4.1,Swift,Firebase,Pagination,Swift4.1,我正在开发一款iOS应用程序,其功能类似于Instagram。我已经成功地在我的个人资料页面上进行了分页,但是在我的主页上总是无法得到正确的结果 目标-按日期按时间顺序获取帖子。最新的应该放在第一位。帖子来自我关注的人。在向下滚动时,先获取3,然后再获取另一个3 示例firebase实时数据库结构 用户表 users -uid -name -emailID 员额表 posts -uid -pid -title -date -pid2 -title

我正在开发一款iOS应用程序,其功能类似于Instagram。我已经成功地在我的个人资料页面上进行了分页,但是在我的主页上总是无法得到正确的结果

目标-按日期按时间顺序获取帖子。最新的应该放在第一位。帖子来自我关注的人。在向下滚动时,先获取3,然后再获取另一个3

示例firebase实时数据库结构

用户表

users
 -uid
  -name
  -emailID
员额表

posts
 -uid
   -pid
    -title
    -date
   -pid2
    -title
    -date
斯威夫特管制员

我得到特定用户的所有UID如下 然后我试着从那些UID那里获取帖子

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

    Database.fetchUserWithUID(uid: uid) { (user) in

        self.fetchPostsWithUser(user: user)

    }
}

这段代码工作起来很奇怪。我没有把帖子整理好。它总是给我随机排列的帖子的输出


我该怎么办?

你随机获得帖子的原因是因为
observeSingleEvent(of:)
以先到先得的方式从数据库中获取数据。因此,在Firebase UI中,它可能是基于创建的字母顺序,但这并不意味着它在获取时会显示在视图中。您必须使用
queryOrderedByKey
)或(
queryOrderedByValue
)并按键/值对其进行排序(如果您通过
childByAutoID
创建帖子,则在创建帖子时,它将自动从最早(在顶部)变为最新。

在self.posts.append和reloadData之间添加以下行将解决您的问题

                self.posts.sort(by: { (p1, p2) -> Bool in
                    return p1.creationDate.compare(p2.creationDate) == .orderedDescending })
1) 。从firebase获取前5个数据我的表名是“Tweets”

2) 。在获取tableview滚动上的下5个数据后

    // Here find last object key from array.
    let key = self.tweetArray[self.tweetArray.count].key

    let FetchtweetRef = FIRDatabase.database().reference()
    FetchtweetRef.child("Tweets").queryOrderedByKey().queryEnding(atValue: key).queryLimited(toLast: 6).observe(.value, with: {(tweets) in

        for  tweet in tweets.children {
            let newTweet = Tweet(snaspshot: tweet as! FIRDataSnapshot)
           // Stored object in Array
        }
        FetchtweetRef.child("Tweets").removeAllObservers()
    })

可以尝试你的解决方案。谢谢大家!@Api42 OP在code ref.queryOrdered(byChild:“creationDate”)中有明确的顺序定义,以便在firebase快照中保持日期顺序。我看不出这段代码有任何真正的错误,在测试应用程序中运行它可以始终如一地工作,并且按照正确的顺序执行它应该执行的操作。但是,我不知道您的creationDate字符串是如何存储在Firebase中的。也可能更容易,而不是反转AllObject来构建数组,只需清除post数组,然后迭代snapshot.children并在位置0插入每个新post。最新的帖子自然会排在首位。此外,您需要确保每个posts节点都包含creationDate子节点,因为如果不是,它将无法正确排序。
                self.posts.sort(by: { (p1, p2) -> Bool in
                    return p1.creationDate.compare(p2.creationDate) == .orderedDescending })
    let FetchtweetRef = FIRDatabase.database().reference()
    FetchtweetRef.child("Tweets").queryOrderedByKey().queryLimited(toLast: 5).observe(FIRDataEventType.value, with: { (tweets) in

        for  tweet in tweets.children{
            let newTweet = Tweet(snaspshot: tweet as! FIRDataSnapshot)
            // Store object in Array
        }

        FetchtweetRef.child("Tweets").removeAllObservers()
    })
    // Here find last object key from array.
    let key = self.tweetArray[self.tweetArray.count].key

    let FetchtweetRef = FIRDatabase.database().reference()
    FetchtweetRef.child("Tweets").queryOrderedByKey().queryEnding(atValue: key).queryLimited(toLast: 6).observe(.value, with: {(tweets) in

        for  tweet in tweets.children {
            let newTweet = Tweet(snaspshot: tweet as! FIRDataSnapshot)
           // Stored object in Array
        }
        FetchtweetRef.child("Tweets").removeAllObservers()
    })