How does the Cloud Firestore Batched writes work in offline mode

感情迁移 提交于 2020-01-15 10:18:26

问题


In the doc about Cloud Firestore Batched writes I read "You can perform batched writes even when the user's device is offline."

Can this be turned off or is it off by default since the text say "can". The Cloud Firestore Transaction will fail if it´s offline and I would like Batched writes to do the same

I get the feeling I have to check for the result in the OnCompleteListener:

batch.commit().addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        // offline/online?? 
    }
});

I think the Doc should contain some explanation on this


回答1:


Firestore transactions exist to make it possible to perform atomic read-modify-write operations from the client. In order to make this guarantee the clients need to be online.

Batched writes are a limited kind of transaction that exists specifically to allow users to be able to change multiple documents in an all-or-nothing manner even while offline.

If you don't need or want to be able to write while offline just use a regular transaction: it will handle checking whether or not you're online and will fail if you're offline. You're not obligated to read anything in the transaction.

Checking the result in the OnCompleteListener for a batch write does not work for this purpose. You couldn't prevent anything in that callback because it is only called once the write has been successfully applied to the server.



来源:https://stackoverflow.com/questions/47073886/how-does-the-cloud-firestore-batched-writes-work-in-offline-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!