【问题标题】:What is the difference between Firebase SetValue() and SetValueAsync() methods and when to use them?Firebase SetValue() 和 SetValueAsync() 方法有什么区别以及何时使用它们?
【发布时间】:2020-02-26 21:35:24
【问题描述】:

官方文档没有显示 SetValue() 和 SetValueAsync() 之间有任何有意义的区别:

https://firebase.google.com/docs/reference/admin/java/reference/com/google/firebase/database/DatabaseReference

1. void setValue(Object value, DatabaseReference.CompletionListener listener)

    Description: Set the data at this location to the given value.


2. ApiFuture<Void> setValueAsync(Object value)

    Description: Set the data at this location to the given value.

使用“异步”方法的含义是什么?

“ApiFuture”是什么意思?

我打算使用这些方法来保存一个字符串。该字符串与一本书的页面一样大。推荐的实现方法是什么?

【问题讨论】:

    标签: java firebase firebase-realtime-database firebase-admin


    【解决方案1】:

    非异步方法将阻塞,直到操作完成。

    异步方法是异步的并立即返回。该工作在一段时间后完成,可以通过返回的 ApiFuture 进行跟踪。

    Learn about asynchronous operations with the Firebase Admin Java SDK.

    Learn about ApiFuture。您向它添加一个侦听器,以便了解操作何时完成。

    异步和非异步方法都可以正常工作。选择最适合您的情况。

    【讨论】:

    • 你确定这是 Java 中的真实行为吗?只是因为setValue 方法有一个completion-listener 参数而询问,该参数会在几秒钟后从某个firebase 线程中调用。同样在调试对setValue() 的调用时,会立即返回
    猜你喜欢
    • 2017-05-18
    • 2020-05-02
    • 2016-03-21
    • 2023-03-17
    • 2014-10-25
    • 2020-04-29
    • 1970-01-01
    • 2010-09-06
    • 2018-07-01
    相关资源
    最近更新 更多