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-使用Firebase分页时重复按键_Ios_Swift_Firebase_Firebase Realtime Database_Pagination - Fatal编程技术网

ios-使用Firebase分页时重复按键

ios-使用Firebase分页时重复按键,ios,swift,firebase,firebase-realtime-database,pagination,Ios,Swift,Firebase,Firebase Realtime Database,Pagination,我正在用Firebase学习分页。我正在使用一种方法,在该方法中,我存储上一页中最后添加的项的键,以便下一页可以从那里继续 问题在于,当使用ref.queryStarting(at value:lastItemKey)从最后添加的键中继续检索项目时,最后一个项目会重复两次(因为queryStarting包含在内) 因此,如果我将查询限制为5,我将只得到4个新项,因为1是重复项 我提出的唯一解决方案是再请求一个项目并删除重复的项目,但我想知道这样做是否有效。(因为我们在每个查询中浪费一项) 如果有

我正在用Firebase学习分页。我正在使用一种方法,在该方法中,我存储上一页中最后添加的项的键,以便下一页可以从那里继续

问题在于,当使用
ref.queryStarting(at value:lastItemKey)
从最后添加的键中继续检索项目时,最后一个项目会重复两次(因为
queryStarting
包含在内)

因此,如果我将查询限制为5,我将只得到4个新项,因为1是重复项

我提出的唯一解决方案是再请求一个项目并删除重复的项目,但我想知道这样做是否有效。(因为我们在每个查询中浪费一项)

如果有任何帮助,我的代码如下所示:

    // rest of the pages 
    if let lastItemID = lastItemKey {
        itemPageRef = self.itemsRef.queryOrderedByKey().queryStarting(atValue: lastItemID)
            .queryLimited(toFirst: UInt(amount))
    } else {
        // First page of data: we retrieve the first (amount) items
        print("We are in the first page of DATA")
        itemPageRef = self.itemsRef.queryOrderedByKey().queryLimited(toFirst: UInt(amount))
    }

    itemPageRef.observeSingleEvent(of: .value, with: { [weak self] (snapshot) in 

请求页面之间的重叠子节点是Firebase API支持的唯一方式。既然没有其他方法可以做到这一点,就没有更有效的方法了

这就是说,它通常非常有效,特别是如果您使用25+子节点的页面大小,这在我看到的大多数用例中也更合理