使用 Firebase 查询 x 和 y 之间的项目
Query items between x and y with 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 的大小。
好的,下面的查询解决了问题。
firebase.childByAppendingPath("topstories").queryOrderedByKey().queryStartingAtValue(String(offset)).queryEndingAtValue(String(offset + batchSize - 1))
问题是 Firebase 文档没有指定 "AnyObject!" 参数可以用作查询的索引。他们还忘记提到索引应该是字符串类型。
根据我们对您的问题的评论,
您可以将 .queryOrderedByKey
与 queryStartingAtValue:
、queryEndingAtValue:
结合使用
/topstories
中的数据存储为数组。按键排序时,数组中对象的索引是键。然后,您需要做的就是将 offset
和 batchSize
转换为字符串并将它们传递给 queryStartingAtValue:
& queryEndingAtValue:
,如下所示:
ref.queryOrderedByKey().queryStartingAtValue(String(offset)).queryEndingAtValue(String(offset+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 个项目。
这是 JavaScript 中实现的相同概念的 example PLNKR。
:)
因此,我正在使用官方 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 的大小。
好的,下面的查询解决了问题。
firebase.childByAppendingPath("topstories").queryOrderedByKey().queryStartingAtValue(String(offset)).queryEndingAtValue(String(offset + batchSize - 1))
问题是 Firebase 文档没有指定 "AnyObject!" 参数可以用作查询的索引。他们还忘记提到索引应该是字符串类型。
根据我们对您的问题的评论,
您可以将 .queryOrderedByKey
与 queryStartingAtValue:
、queryEndingAtValue:
/topstories
中的数据存储为数组。按键排序时,数组中对象的索引是键。然后,您需要做的就是将 offset
和 batchSize
转换为字符串并将它们传递给 queryStartingAtValue:
& queryEndingAtValue:
,如下所示:
ref.queryOrderedByKey().queryStartingAtValue(String(offset)).queryEndingAtValue(String(offset+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 个项目。
- 前 100 项是键 0 到 99。数组中第一项的
这是 JavaScript 中实现的相同概念的 example PLNKR。
:)