【问题标题】:sync vs async firebase operations for swift 4?swift 4的同步与异步firebase操作?
【发布时间】:2018-03-31 02:43:29
【问题描述】:

对 Firebase 实时数据库的所有操作和查询是异步的还是同步的,还是两者兼而有之?

除此之外,Firebase 身份验证呢?

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

【问题讨论】:

    标签: ios firebase grand-central-dispatch


    【解决方案1】:

    关于异步编程的问题在于,一开始它并不是很直观。如果你想获取一些数据,很自然地想要编写如下结构的代码:

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

    这是一个同步调用,简短易懂。 get() 的结果直接从函数返回,调用代码正在等待它完成。但这正是问题所在。你不希望你的代码停下来等待可能需要很长时间的东西。

    iOS/斯威夫特:

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

    如果你问我,我宁愿有一个异步 API 来管理所有后台所需的线程。因此,始终建议将 Firebase 操作放入并发队列,而不是主队列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      相关资源
      最近更新 更多