因此,我使用的是官方的Hacker News API,该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 + offset)的所有项目,然后我将列表的第一个末端减去offset的大小。因此,列表上剩下了batchSize的大小。
好的,因此通过以下查询解决了问题。
firebase.childByAppendingPath("topstories").queryOrderedByKey().queryStartingAtValue(String(offset)).queryEndingAtValue(String(offset + batchSize - 1))
问题是Firebase文档未指定“ AnyObject!”。参数可以用作查询的索引。他们也忘记提到索引应该是String类型。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句