迅捷4的同步vs异步firebase操作? [英] sync vs async firebase operations for swift 4?

查看:39
本文介绍了迅捷4的同步vs异步firebase操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对Firebase实时数据库进行的所有操作和查询都是异步的还是同步的?

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

除此之外,Firebase身份验证又如何?

In addition to this, what about Firebase authentication?

所以我想我的问题是:我是否需要将Firebase操作放入并发队列,还是可以将Firebase操作留在主队列中?

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?

推荐答案

关于异步编程的事情是,一开始它并不是很直观.如果您想获取一些数据,自然可以编写结构如下的代码:

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
}

这是一个同步通话,简短易懂.get()的结果直接从该函数返回,并且调用代码正在等待它完成.但这正是问题所在.您不想让代码停止等待可能需要很长时间的事情.

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:

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
    }
}

如果您问我,我宁愿有一个异步API,该API管理后台所有必需的线程.因此,始终建议将Firebase操作放入并发队列中,而不要放在主队列中.

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.

这篇关于迅捷4的同步vs异步firebase操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆