1

My application determines the speed limit by the user's location and tells the user if he has exceeded it. Starting with Android API level 30 and higher, Google has defined IntentService as deprecated and suggests using WorkManager or JobIntentService and also states that it is necessary to migrate from Firebase JobDispatcher to WorkManager. I see two ways to solve this problem:

  1. Start OneTimeWorkRequest and specify to restart this method periodically in this method while the application is running in the background.
  2. Run PeriodicWorkRequest with a minimum allowed interval of 15 minutes. In this method, run the JobIntentService method, which runs for up to about 10 minutes, but the method may not run or may be destroyed by the system before it is complete.

I'm worried about:

  • potential memory leaks;
  • potential problems with WorkManager or JobIntentService when going from foreground to background and vice versa
  • the ability to use the MVVM pattern
4

1 回答 1

4

我更喜欢使用第二个选项,它可以让您在每次重新安排Worker.

关于您的担忧:

  • 唯一可能的泄漏是不当的位置回调。这可以很容易地追踪到,你不应该太担心它。
  • 计划Worker被放入数据库并独立于应用程序执行。这意味着,应用程序的用户可见性没有任何影响。在您的情况下,我假设您希望在用户恢复应用程序后取消您的预定工作,也就是说,您可以将 a 分配tagWorker并清除任何对用户可见的预定或正在进行的工作。
  • 我更喜欢将Workers 与 MVVM 隔离,并且只在 worker 中注入用例/交互器并执行用例/查询交互器。WorkerManager提供了很好的 API 来查询Worker状态,您可能需要在以前的实现和 API >= 30 之间编写共同点。将其Worker视为您的用例的不同执行容器。
于 2020-12-07T16:42:52.053 回答