Home How to model nested data structure when reading from Firebase Swift 3
Reply: 0

How to model nested data structure when reading from Firebase Swift 3

bibscy
1#
bibscy Published in 2017-12-07 22:36:13Z

How can I remodel struct FireBaseData so I that I can read the values of claimed and cleanerUID that are nested? -cancelledBy: is optional, it may or may not exist in the database.

-Cleaner
 -UID
  -bookings
     -booking1
        -name: "John"
        -amount: "30"
        -cancelledBy:
           -timeStamp:
             -claimed:false
             -cleanerUID:8203NKDHF2921
  -booking2

 struct FireBaseData {
   let name:String
   let amount:String
   let ref:FIRDatabaseReference?

 init(snapshot:FIRDataSnapshot){
    if let nameReceived = (snapshot.value! as? NSDictionary)?["name"] as? String {
        name = nameReceived
    }

      if let amountReceived = (snapshot.value! as? NSDictionary)?["name"] as? String {
        amount = amountReceived
     }
   }
}


class DisbursePayment {

    override func viewDidLoad(){

    dbRef.child(uid).child("bookings").observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in
        var finalBookings = [FireBaseData]()

            for booking in snapshot.children{
                let bookingItem = FireBaseData(snapshot: booking as! FIRDataSnapshot)
                    finalBookings.append(bookingItem)
            }

      }, withCancel: { (Error:Any) in
        print(error)
    })

    dbRef.removeAllObservers()
 }//end of viewDidLoad

}//end of class
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.31652 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO