2017-07-17 149 views
0

我正在处理一个大型Firebase数据库,每个节点至少有6层层次结构以及许多子节点。我想解析整个快照并将其转换为对象模型。我发现this解决方案,但在我看来,它是非常低效的解析每个节点的孩子requries调用firebase,这会以指数方式增加延迟。有没有什么方法可以在本地完成“ref.observeSingleEvent”,而不是打电话给Firebase?任何其他更好的替代品将不胜感激。Swift Firebase快照到对象模型

+0

是的,你可以下载一个地图在火力地堡与下它的许多childerns,你不必做超过1个呼叫火力地堡如果所有childern都落在1张地图下,并且您下载了该地图。 –

回答

1
//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 
      } 
     } 
    } 

图片1: enter image description here

图2: enter image description here

+0

这导致了很多臃肿的代码,但工作方式比其他解决方案更好。 – tectonicpie

+0

@tectonicpie是的我知道,但我认为没有其他办法(如果有的话,我想知道),但它主要取决于你如何构造你的数据。 –

+0

如何使此解决方案与对象数组一起工作?例如 “name”:[{object1},{object2},{object3}] 我可以将名称及其数组转换为NSDictionary,但一旦进入数组内部,就没有键值对,只是对象的值。 – tectonicpie