Swift Firebase快照到对象模型

时间:2017-07-17 11:17:09

标签: ios swift firebase firebase-realtime-database

我正在使用一个包含至少6层层次结构的大型firebase数据库以及每个节点的许多子项。我想解析整个快照并将其转换为对象模型。我找到了this解决方案,但在我看来,解析每个节点的孩子需要对firebase进行调用时效率极低,这会以指数方式增加延迟。是否有任何方法可以使用##; ref.observeSingleEvent"要在本地完成,而不是打电话给firebase?任何其他更好的选择将非常感激。

1 个答案:

答案 0 :(得分: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