ios - 使用 Firebase 查询 x 和 y 之间的项目

标签 ios swift firebase

因此,我正在使用官方的 Hacker News API,它托管在 Firebase 上。

我遇到的问题是我基本上想获得列表的一个子集。

一路走来的东西。 让 topNewsRef = firebase.childByAppendingPath("topstories").queryLimitedToFirst(UInt(batchSize + offset)).queryLimitedToLast(UInt(batchSize))

[我知道这行不通,但我想要这种效果]。基本上我想要一个集合的子集,由一个范围指定;例如,从第 2 项到第 15 项]。

假设我想要第 75 项中的 50 项,但上面的方法不起作用。所以问题是;我怎样才能达到同样的效果?

例如;在 Firebase 中给定一个包含 100 个项目的列表。我想要第 50 和 75 号的所有项目。没有任何属性会泄露项目的顺序。

这是我目前的解决方案;

        let topNewsRef = firebase.childByAppendingPath("topstories").queryLimitedToFirst(UInt(batchSize + offset))
    var handle: UInt?
    handle = topNewsRef.observeEventType(.Value) { (snapshot: FDataSnapshot!) -> Void in
        if let itemIDs = snapshot.value as? [Int] {
            itemIDs.dropFirst(offset) // This drops all items id I already fetched ...
            for itemID in itemIDs {
                let itemRef = self.firebase.childByAppendingPath("item/\(itemID)")
                var itemHandle: UInt?
                itemHandle = itemRef.observeEventType(.Value, withBlock: { (snapshot: FDataSnapshot!) -> Void in
                    if let itemHandle = itemHandle {
                        itemRef.removeObserverWithHandle(itemHandle)
                    }

                    if let json = snapshot.value as? [String:AnyObject],
                        // Handle JSON ...
                    }
                })
            }
        }
        if let handle = handle {
            topNewsRef.removeObserverWithHandle(handle)
        }
    } // offset += batchSize

...这是获取从开始(偏移量)到结束(batchSize + 偏移量)的所有项目,然后我将列表的第一个末端删除偏移量的大小。因此在列表中留下了 batchSize 的大小。

最佳答案

根据我们对您的问题的评论,

您可以使用 .queryOrderedByKey结合 queryStartingAtValue:, queryEndingAtValue:

/topstories 中的数据存储为数组。按键排序时,数组中对象的索引是键。然后,您需要做的就是将 offsetbatchSize 转换为字符串并将它们传递给 queryStartingAtValue: & queryEndingAtValue:像这样:

ref.queryOrderedByKey().queryStartingAtValue(String(offset)).queryEndingAtValue(String(offs‌​et+batchSize-1));
  • 如果您按批大小增加偏移量,请不要忘记将 endingValue 减去 1。
  • 对于系列中的第 n 个查询,
    • startingAtIndex = initialOffset + (batchSize * (n - 1))
    • endingAtIndex = initialOffset + (batchSize * n) - 1
  • 例如,设batchSize=100,初始offset=0。
    • 前 100 项是键 0 到 99。数组中第一项的 key(索引)是 0
    • 如果您查询 endingAt(offset+batchSize),您将在索引 100 处结束,这是数组中的第 101 个项目。
    • 因此,您将获得 101 件商品——比您的批量多一件。
    • 第二个查询将针对索引为 100-199 的项目(从 0+batchsize 开始到 0+batchsize+batchsize-1 结束),即 100 个项目。

这是一个 example PLNKR在 JavaScript 中实现了相同的概念。

:)

关于ios - 使用 Firebase 查询 x 和 y 之间的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32093242/

相关文章:

ios - UITabBarController 在 iPhone 5 模拟器上没有响应 : 4-inch retina display

swift - Swift 3.1 中没有空或通配符的详尽 catch block

java - 如何在 Android 的一个 FirestoreRecyclerAdapter 中包含两个模型类轨道和艺术家?

Swift,是否可以有一个 IBInspectable block ?

javascript - 使用 ControllerAs 语法的 Firebase 3 向数据绑定(bind)

javascript - 谷歌 polymer 和firebase文档使用javascript更改数据?

ios - 使 UICollectionView 看起来像 UITableView

ios - 获取有关 CGpath、UIBezierPath 的信息?

ios - iOS app开发,是否可以检测相机是否拍过照片?

ios - 无论 NSFetchedResultsController 中的 sectionNameKeyPath 如何,都更改排序顺序