我正在使用一个大型 Firebase 数据库,该数据库至少有 6 层层次结构,并且每个节点有许多子节点。我想解析整个快照并将其转换为对象模型。我发现this https://stackoverflow.com/questions/35514497/firebase-in-swift-nested-query-not-working-properly解决方案,但在我看来,它的效率极低,因为解析每个节点的子节点都需要调用 firebase,这会呈指数级增加延迟。有没有办法让“ref.observeSingleEvent”在本地完成,而不是调用 firebase?任何其他更好的选择将不胜感激。
//this goes into your call (observeSingleEvent)
let enumerator = snapshot.children //assuming you use snapshot as name
while let rest = enumerator.nextObject() as? FIRDataSnapshot {
// this loops through every child in that map
let values = (rest as! DataSnapshot).value as? NSDictionary
let coins= values?["coins"] as? Int ?? 0
//above code looks for a key with username and grabs the value from that. If it is not a string value it returns the default value.
//use above code for picture 1
if (rest as! DataSnapshot).key == "slot"{
let enumeratorMap1 = (rest as! DataSnapshot).children
while let rest2 = enumeratorMap1.nextObject() as? FIRDataSnapshot {
let valuesMap1 = (rest2 as! DataSnapshot).value as? NSDictionary
//loop through values in new map
//use this methodes for looping through maps, as stated in picture 2
//keep repeating this method for any child under the map
}
}
}
Picture 1:
Picture 2:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)