sync vs async firebase operations for swift 4?

房东的猫 提交于 2019-12-14 04:06:42

问题


Are all operations and queries onto a Firebase realtime database asynchronous or synchronous or both?

In addition to this, what about Firebase authentication?

So I guess my question is: Do I need to put Firebase operations into a concurrent queue, or is it okay just leaving it in the main queue?


回答1:


The thing about asynchronous programming is that it’s not really intuitive at first. If you want to fetch some data, it’s natural to want to write code that’s structured something like this:

try {
    result = database.get("the_thing_i_want")
    // handle the results here
}
catch (error) {
    // handle any errors here
}

This is a synchronous call, and it’s short and easy to understand. The result of get() is being returned directly from the function, and the calling code is waiting for it to complete. But this is precisely the problem. You don’t want your code to stop to wait for something that could take a long time.

iOS/Swift:

Firestore.firestore().document("users/pat")
        .getDocument() { (snapshot, err) in
    if let snapshot = snapshot {
        // handle the document snapshot here
    }
    else {
        // handle any errors here
    }
}

If you ask me, I’d rather have an asynchronous API that manages all the required threading behind the scenes. So it's always suggested to put Firebase operations into a concurrent queue, not in the main queue.



来源:https://stackoverflow.com/questions/49579427/sync-vs-async-firebase-operations-for-swift-4

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