简体   繁体   English

Swift Firebase快照到对象模型

[英]Swift Firebase Snapshot to object model

I am working with a large firebase database with at least 6 layers of hierarchy as well as many children for each node. 我正在使用大型Firebase数据库,该数据库至少具有6层层次结构,并且每个节点有许多子级。 I wanted to parse the entire snapshot and convert it into an object model. 我想解析整个快照并将其转换为对象模型。 I found this solution but in my opinion it is extremely inefficient as parsing each node's children requries a call to firebase and this increases latency exponentially. 我找到了这种解决方案,但是我认为解析每个节点的子级需要调用Firebase的方式效率极低,并且延迟成倍增加。 Is there any way for the "ref.observeSingleEvent" to be done locally instead of making a call to firebase? 有什么办法可以在本地完成“ ref.observeSingleEvent”而不是调用firebase吗? Any other better alternatives would be much appreciated. 任何其他更好的选择将不胜感激。

//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: 图片1: 在此处输入图片说明

Picture 2: 图片2 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM