如果有数据仍在排队存储,应用程序关闭后,保持与Firebase实时数据库的连接? [英] Maintaining connection to Firebase realtime database after app is closed if there is data that is still queued to be stored?

查看:253
本文介绍了如果有数据仍在排队存储,应用程序关闭后,保持与Firebase实时数据库的连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android上使用了Firebase的实时数据库,并且我了解其工作原理的方式是,即使应用程序与网络断开连接,Firebase也会对用户启动的事务进行排队,然后在连接恢复时执行。这工作得很好,但如果应用程序关闭,这个队列似乎被丢弃。

处理离线功能的Firebase文档列出了以下内容:
$ b


事务在应用程序重新启动之间不会持久


$ b 即使启用了持久性,事务也不会持续在
应用程序重新启动之间。因此,您不能依靠离线进行的事务将
委托给您的Firebase实时数据库。为了提供最佳用户
的体验,应用程序应该显示交易尚未保存到您的Firebase实时数据库中,或者确保您的应用程序
手动记住它们,然后再次执行它们一个应用程序重新启动。

但据我所知,没有办法知道数据是否已经写完成数据库。

你应该如何手动记录还需要写入数据库的应用程序?是否有某种方式访问​​尚未执行的事务队列?或者有什么办法保持应用程序在后台运行后关闭,可以同步连接恢复数据?



预先感谢。

解决方案


但据我所知,没有办法知道数据是否完成写入数据库。

实际上是。 Transaction.Handler 接口有一个[ onComplete 方法]( https://firebase.google。 com / docs / reference / android / com / google / firebase / database / Transaction.Handler.html#onComplete(com.google.firebase.database.DatabaseError ,boolean,com.google.firebase.database.DataSnapshot) )传递给该参数的 boolean 是一个标志,用于指示事务是否被提交:


已提交

如果事务成功完成,则为true;如果中止或发生错误,则为false

有关更多信息,请参阅关于tra的Firebase文档nsactions


I'm using Firebase's realtime database on Android and the way I understand how it works is that even if the app disconnects from the network, Firebase will simply queue the transactions that the user has initiated and then perform then when connectivity is resumed. This works really well but if the app is closed then this queue seems to be discarded.

The Firebase docs on handling offline capabilities states the following:

Transactions are not persisted across app restarts

Even with persistence enabled, transactions are not persisted across app restarts. So you cannot rely on transactions done offline being committed to your Firebase Realtime Database. To provide the best user experience, your app should show that a transaction has not been saved into your Firebase Realtime Database yet, or make sure your app remembers them manually and executes them again after an app restart.

But as far as I know, there is no way of knowing whether or not data has finished being written to the database.

How exactly would you go about making the app manually remembering what still needs to be written to the database? Is there some way of accessing the queue of transactions that is yet to be carried out? Or is there some way of keeping the app running in the background after being closed that could just sync the data when connectivity resumes?

Thanks in advance.

解决方案

But as far as I know, there is no way of knowing whether or not data has finished being written to the database.

There actually is. The Transaction.Handler interface has a [onComplete method](https://firebase.google.com/docs/reference/android/com/google/firebase/database/Transaction.Handler.html#onComplete(com.google.firebase.database.DatabaseError, boolean, com.google.firebase.database.DataSnapshot)). The boolean that is passed to that argument is a flag to indicate if the transaction was committed:

committed
True if the transaction successfully completed, false if it was aborted or an error occurred

For more information, see the Firebase documentation on transactions.

这篇关于如果有数据仍在排队存储,应用程序关闭后,保持与Firebase实时数据库的连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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