Closure for Firebase Realtime Database not finishing before executing root view controller

7 hours ago 1
ARTICLE AD BOX

I need to acquire a set of data prior to the execution of the root view controller. I have the following code setup in order to get the data but it seems the ViewWillAppear still fires before the data execution is complete. I have added it to the AppDelegate and that didn't work either.

Data aquisition:

func fetchData(completion: @escaping (Data?, Error?) -> Void) { var retVal: Int = 0 myDict.removeAll() var ref: DatabaseReference! ref = Database.database().reference() let postsRef = ref.child("Rcd198D/Counts/") postsRef.observeSingleEvent(of: .value, with: { (snapshot) in for child in snapshot.children.allObjects as! [DataSnapshot] { let myName = child.key let myValue = child.value myDict[myName] = myValue as? Int print(myDict ) DispatchQueue.main.async { completion(data, error) // add data to screen. } }.resume() }) }

If I'm missing something please let me know and I'll be happy to add to it.

Read Entire Article