In Swift, I am using an observerEventType
to retrieve snapshot data from Firebase, and return it in a function. But it appears that the data isn't being saved from the snapshot.
func checkAmount() -> Int {
var amount = -1
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.rootRef.child("users/").child(NSUserDefaults.standardUserDefaults().stringForKey("id")!).observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
Amount = (snapshot.value!.objectForKey("CoinAmount") as? Int)!
})
})
return Amount
}
But in my viewDidLoad() function when I do:
if checkAmount() == 0 {
// You can do this
} else {
// or you can do that
My logical was that by using dispatch_async
I would be able to save the observeEvent (which I was informed is called in the background thread), and use that data in the rest of my program.