What is the best way to schedule a task in react native?

本小妞迷上赌 提交于 2019-12-31 09:16:14

问题


There are different ways to run tasks in the background in Android and iOS respectively. I have found What is the best way to schedule task in android? as well on stack overflow.

I am wondering which is the best way using react-native only. Would it be good enough to use setInterval or setTimeout for tasks that have to run daily or every few hours?

Would not those tasks be killed by the OS?

Any ideas or suggestions?


回答1:


I will answer my own question to see if this information can be of used by anyone looking for it.

Since the different mobile OSs tend to kill background jobs, or stall them to save battery, there are few deterministic methods to schedule tasks in react native. I use a combination of the following:

  • Offload timers to the background, which work with the app in both fore and background https://github.com/ocetnik/react-native-background-timer (!If you use create-react-native-app you must eject it)

  • Use a background-fetch for iOS and HeadlessTask in Android, here is a decent library https://github.com/jamesisaac/react-native-background-task

  • Use geolocation updates to wake up the app and start threads https://github.com/mauron85/react-native-background-geolocation.

  • I guess you can follow similar strategies using bluetooth wake-ups.

  • Push notifications from a server to ensure deterministically that the app wakes app (except it having been killed by the OS). In iOS, ensure that you call notification.finish() to avoid being discriminated by the task handler algorithm.

  • For Android you can try to use AlarmManager API https://github.com/vikeri/react-native-background-job.

Beware of the dragons: your app might be closed if it abuses execution time or memory usage after a system wake up. You may have to rehydrate all listeners after the phone was left without battery. So the user still needs to interact heavily with your app.

Update: From Android O there are very strict background execution limits. When using a HeadlessJSTask service, ensure that it is launched as a foreground service if you want it to last longer than a few seconds. It may require a notification with it. Take into account that only loading the bundle can take up to a few seconds, depending on your app and the device.




回答2:


As a matter of fact there is not any sufficient way for that. but we can remark mauron85 as a way which is better then others on android but also it doesn't work perfectly on IOS. for example if app has killed by user the job would not keep working or there is not any control of job execution quantity the job fires each time device changes its position.

other components like react-native-background-fetch and react-native-background-task have the limitation of job execution period(the job repeats after each 15 minutes and there is no way to decrease this time period) and they just work on android.

it would be great if Facebook react native has some practical solution for this problem.



来源:https://stackoverflow.com/questions/39946431/what-is-the-best-way-to-schedule-a-task-in-react-native

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