[英]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.