I would like the in loop to send a bunch of network requests to firebase, and then pass the data to the new view controller after the method finishes. Here is my code:
var datesArray = [String: AnyObject]() for key in locationsArray { let ref = Firebase(url: "http://myfirebase.com/" + "\(key.0)") ref.observeSingleEventOfType(.Value, withBlock: { snapshot in datesArray["\(key.0)"] = snapshot.value }) }
I have a couple of problems. First, how do I wait for the for loop to complete and all network requests to complete? I cannot change the Watch for SingleEventOfType function, it is part of the Firebase SDK. Also, will I create some kind of race condition trying to access dateArray from different iterations of the for loop (hope this makes sense)? I read about GCD and NSOperation, but I got a little confused as this is the first application I created.
Note: Locations array is an array containing the keys that I need to access firebase. It is also important that network requests start asynchronously. I just want to wait until ALL asynchronous requests complete before I pass the dateArray to the next view controller.
asynchronous swift grand-central-dispatch nsoperation
Josh Mar 10 '16 at 2:37 2016-03-10 02:37
source share