Swift Firebase Snapshot to object model

前端 未结 1 1438
被撕碎了的回忆
被撕碎了的回忆 2021-01-17 01:25

I am working with a large firebase database with at least 6 layers of hierarchy as well as many children for each node. I wanted to parse the entire snapshot and convert it

相关标签:
1条回答
  • 2021-01-17 01:39
    //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:

    0 讨论(0)
提交回复
热议问题