How to run ObverveEventType in Main Thread?

删除回忆录丶 提交于 2019-12-25 08:09:31

问题


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.

来源:https://stackoverflow.com/questions/38547875/how-to-run-obverveeventtype-in-main-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!