简体   繁体   English

如果未调用活动的 onDestroy() 方法,如何更新远程数据库中的状态?

[英]How to update status in remote database if the activity's onDestroy() method is not called?

The goal is to update status in a remote database once the activity gets destroyed.目标是在活动被破坏后更新远程数据库中的状态。 The difficulty is that the activity's onDestroy() method call can be skipped if system kills the process.困难在于,如果系统终止进程,则可以跳过活动的 onDestroy() 方法调用。 The methods onStop() and onPause() are of no interest, since they don't ensure the activity finalization. onStop() 和 onPause() 方法没有意义,因为它们不能确保活动完成。 ActivityResult in other activity would not work either, because the app could be killed.其他活动中的 ActivityResult 也不起作用,因为该应用程序可能会被终止。 Service might be the solution, but I'm afraid it gets killed along with the activity (eg onTaskRemoved() in Service is not called in case an app is force-stopped). Service 可能是解决方案,但恐怕它会随着活动一起被杀死(例如,如果应用程序被强制停止,则不会调用 Service 中的 onTaskRemoved())。 What could solve the problem?什么可以解决问题?

The best solution I have come up with so far is the use of Firebase Cloud Functions together with Google Cloud Functions.到目前为止,我想出的最佳解决方案是将 Firebase Cloud Functions 与 Google Cloud Functions 结合使用。

The short story long长话短说

Since the requirement is to guarantee a method run no earlier than the activity's onDestroy() callback, an attractive solution is Android Jetpack's WorkManager for由于要求保证方法不早于活动的onDestroy()回调运行,一个有吸引力的解决方案是 Android Jetpack 的WorkManager for

WorkManager is an API that makes it easy to schedule reliable, asynchronous tasks that are expected to run even if the app exits or the device restarts. WorkManager 是一个 API,它可以轻松安排可靠的异步任务,即使应用程序退出或设备重新启动,这些任务也有望运行。

However, lets imagine that the phone have blown up.但是,让我们想象一下手机已经炸毁了。 Then the task will be run NEVER.然后任务将永远不会运行。 Which is ok, if it is not meant to change the remote database state especially when the state is shared between multiple users.如果不是要更改远程数据库状态,尤其是在多个用户之间共享状态时,那没关系。

Therefore, the safest way to work with remote databases is to schedule remote functions as a backup solution or base your program logic on that.因此,使用远程数据库的最安全方法是将远程功能安排为备份解决方案或基于此解决方案逻辑。 The remote functions most probably would be written in other language than your program, but there is nothing to be scared of and totaly worth to do.远程函数很可能是用你的程序以外的其他语言编写的,但没有什么可害怕的,完全值得做。

This article helped me How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL) .这篇文章帮助我如何安排 Cloud 函数在未来使用 Cloud Tasks 运行(构建 Firestore 文档 TTL) In case the remote database is Firebase (Firestore or Realtime), it is possible to write Cloud Functions, which would be triggered on database changes and schedule Google Cloud Tasks.如果远程数据库是 Firebase(Firestore 或 Realtime),则可以编写 Cloud Functions,这将在数据库更改时触发并安排 Google Cloud Tasks。 The latter can run HTTP triggered Firebase Cloud Function and do whatever is needed, eg update the database values, or check for some condition and reschedule the Google Cloud Task.后者可以运行 HTTP 触发的 Firebase Cloud Function 并执行任何需要的操作,例如更新数据库值,或检查某些条件并重新安排 Google Cloud 任务。 So, even if a phone blows up, the scheduled task will be executed (again, if servers would not malfunction..).所以,即使手机坏了,预定的任务也会被执行(同样,如果服务器不会出现故障......)。 The functions can be written in JavaScript or TypeScript, wich is pretty easy to grasp.这些函数可以用 JavaScript 或 TypeScript 编写,非常容易掌握。

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

相关问题 在onHandleIntent之后立即调用IntentService的onDestroy方法 - IntentService's onDestroy method getting called straight after onHandleIntent 如何在主要活动中访问PreferenceActivity的onDestroy()? - How to access PreferenceActivity's onDestroy() within the main activity? 活动onDestroy永远不会在OutOfMemoryError之前调用 - Activity onDestroy is never called before OutOfMemoryError 如何在onDestroy中执行方法? - How to execute a method in onDestroy? 是否有可能在onDestroy之后调用回调方法? - Is it possible for a callback method to be called after onDestroy? 如何在简单的非活动Java类中创建类似于onDestroy的方法? - How do I create a method similar to onDestroy in a simple non-activity java class? 当其他活动打开时,从未调用过OnPause()和onDestroy() - OnPause() and also onDestroy() never called when other activity opened 当我启动 Activity 时,在 onCreate() 之后调用 OnDestroy() - OnDestroy() is being called after onCreate() when I start Activity 在Activity的onDestroy()中使用Runtime.getRuntime()。gc()方法是一种好习惯吗? - Is it a good practice to use Runtime.getRuntime().gc() method in onDestroy() of an Activity? 在调用onDestroy()之后,我的数据库删除了所有行 - My database deletes all rows after onDestroy() is called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM