0

I'm trying to pass data that I've retrieved from my firebase database into a field of my singleton. The data is received via closure, and in that closure I'm passing some data into my singleton's properties. If I print the data structure inside the closure (after everything's been assigned) I get the output I'm expecting, but if I print it at the end of the initializer after all the data should've been passed in, it's empty.

import Foundation
import Firebase

class EmployeeList {
    static let sharedInstance = EmployeeList()
    var employeeDictionary: [String: [EmployeeData]]
    var ref: DatabaseReference!

    private init() {
        employeeDictionary = [String: [EmployeeData]]()
        ref = Database.database().reference()

        ref.child("employeeList").observeSingleEvent(of: .value, with: { snapshot in
            if let dictionary = snapshot.value as? [String: [String: AnyObject]] {
                for subsection in dictionary {
                    var subsectionEmployees: [EmployeeData] = []

                    for item in subsection.value {
                        self.ref.child("employeeList/\(subsection.key)/\(item.key)").observeSingleEvent(of: .value, with: { employeeSnapshot in
                            let employeeObject = EmployeeData(snapshot: employeeSnapshot)
                            subsectionEmployees.append(employeeObject)
                            self.employeeDictionary[subsection.key] = subsectionEmployees
                            //print(self.employeeDictionary) This print statement prints out the expected data every time another employee is appended
                        })
                    }
                }
            }
            //print(self.employeeDictionary) This print statement prints an empty data structure
        })
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

get data from Firebase as Below

var messagedata = [String:AnyObject]()

 let databaseReff = Database.database().reference().child("message")

 databaseReff.queryOrdered(byChild: "fromId").queryEqual(toValue: self.recieverId).observe(.value, with: { snapshot in
      if snapshot.exists(){
           self.messagedata = snapshot.value! as! [String : AnyObject]        
           self.getAllMessagesSent(snapshot: self.messagedata)
      } else 
           self.getAllMessagesSent(snapshot: self.messagedata) //Function Created
      }
 })

pass the data fetched from Clousre to a dictionary and pass that dict to a function and do anything you want to do or use escaping blocks

func getAllMessagesSent(snapshot: [String:AnyObject]) {
     //data is here
}
Jay
  • 34,438
  • 18
  • 52
  • 81
iOS Geek
  • 4,825
  • 1
  • 9
  • 30
  • A couple of things: You can't reference the var messageData within the closure using self. It would need to be outside of the function enclosing the let databaseREff statement. Also, once the value is typecast as [String: AnyObject] it's really no longer a snapshot - just a naming thing but changing that parameter name would avoid confusion. – Jay Aug 01 '17 at 18:01
  • I think we could refer and I had even implemented this coded I Ama also not getting issue too and yes I am converting snapshot to [string:anyobject] to get a reference we could use a array too here. but I prefer using [string:anyobject] because I get full Snapshot at once and sort it. later on as per my requirement – iOS Geek Aug 02 '17 at 04:17