简体   繁体   English

在Firebase上使用.setPersistenceEnabled(true)时,真的需要.addOnCompleteListener吗?

[英]It's really necessary .addOnCompleteListener when you're using .setPersistenceEnabled(true) on Firebase?

thanks for curiosity ! 感谢您的好奇心!

I'm in doubt if it's really necessary to call addOnCompleteListener when I'm using setPersistence(true) . addOnCompleteListener在使用setPersistence(true)时是否真的有必要调用addOnCompleteListener

When I use addOnCompleteListener , if my internet is offline, the screen keep loading because the addOnCompleteListener will never end - waiting for connection. 当我使用addOnCompleteListener ,如果我的Internet处于脱机状态,则屏幕将继续加载,因为addOnCompleteListener将永远不会结束-等待连接。 So, I can't add anything offline because the loading screen is waiting for connection (breaking the concept of persistence). 因此,我无法离线添加任何内容,因为加载屏幕正在等待连接(破坏了持久性的概念)。

Example below shows what I'm talking about : 以下示例显示了我在说什么:

getDatabaseReference()
        .child("Example")
        .child(userID)
        .push()
        .setValue(dataExample)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        if (task.isSuccessful()){
            //interface call with success response
            interface.onSuccess();
        }else {
            //interface call with failure response
            interface.onFailure(task.getException().getMessage());
        }
    }
});

So, as I said, if my internet is offline, I can't complete the action, the loading screen keeps loading forever (waiting for completelistener). 因此,正如我所说,如果我的Internet处于离线状态,我将无法完成操作,因此加载屏幕会永远加载(等待completelistener)。

I figured out, that without addOnCompleteListener , I can add and remove values while I'm offline, because they're in cache, so when I have internet, the app send the updates to database online - that's brilliant. 我发现,没有addOnCompleteListener ,我可以在脱机时添加和删除值,因为它们在缓存中,所以当我有互联网时,该应用程序会将更新在线发送到数据库中-很棒。

The example below shows what I am proposing: 下面的示例显示了我的建议:

 getDatabaseReference()
        .child("Example")
        .child(userID)
        .push()
        .setValue(dataExample);

 //interface call with successful response without handling error (because it won't happen)
 interface.onSuccess();

So, my question is, it's right to do it ? 所以,我的问题是,这样做正确吗? It's a good practice ? 这是一个好习惯吗? Without addOnCompleteListener , and stay able to use my app offline, sending updates only when able to ? 如果没有addOnCompleteListener ,并且可以脱机使用我的应用程序,则仅在有能力时发送更新?

What you are seeing is the expected behavior. 您所看到的是预期的行为。 A write to RealtimeDatabase isn't considered "complete" until it reaches the server and becomes visible to other clients. 直到对RealtimeDatabase的写入到达服务器并且对其他客户端可见之后,才认为它是“完成”的。 A locally cached write is not considered complete, until the SDK manages to synchronize it. 在SDK设法同步之前,本地缓存的写入才被认为是完整的。 If you don't need to know when the write is finally received by the server, or if it was successful, then there is no need to register a listener with the Task it generates. 如果您不需要知道服务器最终何时接收到写操作或写操作是否成功,则无需在其生成的Task中注册侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 什么是不可修改的地图(真的)是必要的? - When is the unmodifiablemap (really) necessary? 从 firebase 获取用户时完全跳过 addOnCompleteListener - addOnCompleteListener is completely skipped when getting user from firebase 是否真的需要删除完整的侦听器Firebase身份验证? - Is it really necessary to remove the complete listener Firebase Authentication? 使用 orderByChild() 时 addChildEventListener、addValueEventListener 和 addOnCompleteListener 之间的区别? - Difference between addChildEventListener, addValueEventListener, and addOnCompleteListener when using orderByChild()? 何时使用AtomicReference(Java)? 真的有必要吗? - When to use AtomicReference (Java)? Is it really necessary? 为特定的DatabaseReference设置Firebase.setPersistenceEnabled() - Set Firebase.setPersistenceEnabled() for particular DatabaseReference 你应该在 addOnCompleteListener 里面写什么? - What are you supposed to write inside addOnCompleteListener? 在这种情况下,仅调整图像大小(png)时,BitmapFactory真的必要吗? - Is BitmapFactory really necessary when it comes to only resizing images (png) in this scenario? 在解析byte []时是否真的需要指定String编码? - Is specifying String encoding when parsing byte[] really necessary? 如何配置JVM仅在确实需要时才分配内存? - How to configure JVM to allocate memory only when is really necessary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM