如果Firestore无法保存数据会怎样? [英] What happens if firestore fails to save data

查看:39
本文介绍了如果Firestore无法保存数据会怎样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1 假设我正在做以下操作

1 Suppose that i am doing something like following

FirestoreDatabase.getInstance()
                 .collection("users")
                 .add(userData); // where userData is reference to UserData object which contains many fileds 

现在,如果我在recyclerview中显示该数据,则客户端库将立即在recyclerview中显示该数据,甚至在将其保存到firestore数据库中之前,也可以保持我的应用程序的响应速度.

Now if i am showing that data in recyclerview , the client library will show that immediately in recyclerview even before it is saved in firestore database to keep my app more responsive.

因此,当数据显示在recyclerview中时,用户认为他已成功添加数据,如果数据到达Firestore时写请求失败,将会发生什么情况, onFailure 列表触发,并且由于实际写入之前UI中已有数据,数据会从UI中消失?

So when the data shows up in the recyclerview the user thinks he successfully added the data BUT what happens if the write request fails when the data reach the firestore , will the onFailure listner trigger and as the data already in the UI before actual write wil the data disappear from the UI ?

2 当用户离线时,他仍然可以使用该应用并添加数据并显示在用户界面中,这意味着他成功添加了数据,然后添加了 onSuccessListener 仅当用户在线时才会被触发.

2 When the user is offline he can still use the app and add the data and shows up in UI for user it means he successfully added the data then whey onSuccessListener gets triggered only when the user is online.

ex 假设成功侦听器正在显示"已添加数据"的祝酒词,但用户仅在在线时才看到它,而在离线时他可以在UI中看到数据,但不敬酒,并思考为什么这次没有出现敬酒.

ex supposein successlistener in am displaying a toast saying data added but user only sees it when he is online , when he is offline he can see the data in UI but not toast and thinks why toast didn't show up this time.

推荐答案

但是如果写入请求失败会怎样

BUT what happens if the write request fails

这意味着无权写入该数据. onFailure()仅在写入文档时出现问题时执行,这意味着写入操作被拒绝.

It means that don't have access to write that data. onFailure() is only executed when there was a problem with writing the document, meaning the write operation is rejected.

用户离线时,他仍然可以使用该应用程序并添加数据并显示在用户界面中

When the user is offline he can still use the app and add the data and shows up in UI

之所以发生这种情况,是因为对于Android和iOS,离线持久性是默认情况下启用.

This is happening because, for Android and iOS, offline persistence is enabled by default.

onSuccessListener仅在用户在线时触发.

onSuccessListener gets triggered only when the user is online.

是的, onSuccessListener()仅在写入操作成功时触发,这意味着该操作是在Firebase服务器上写入的.

Yes, onSuccessListener() gets triggered only when the write operation is successful, meaning the operation is written on Firebase servers.

upposein成功侦听器正在显示一个祝酒词,说添加了数据,但用户仅在在线时才看到它;当他离线时,他可以在UI中看到数据,但看不到祝酒词,并思考为什么这次没有出现祝酒词./p>

upposein successlistener in am displaying a toast saying data added but user only sees it when he is online , when he is offline he can see the data in UI but not toast and thinks why toast didn't show up this time.

不会向用户发送该通知,因为该数据尚未添加到服务器上.

The user won't be notified with that toast, because that data is not yet added on the server.

这篇关于如果Firestore无法保存数据会怎样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆