问题描述
在 Android 和 iOS 中分别有不同的在后台运行任务的方式.我发现 在android?以及堆栈溢出.
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.
我想知道仅使用 react-native
的最佳方法是什么.对于必须每天或每隔几个小时运行的任务,使用 setInterval
或 setTimeout
是否足够好?
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?
有什么想法或建议吗?
推荐答案
我将回答我自己的问题,看看这些信息是否可供任何寻找它的人使用.
I will answer my own question to see if this information can be of used by anyone looking for it.
由于不同的移动操作系统倾向于终止后台作业,或停止它们以节省电池,因此在 React Native 中几乎没有确定性的方法来安排任务.我使用以下组合:
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:
将计时器卸载到后台,可在前台和后台与应用程序一起使用 https://github.com/ocetnik/react-native-background-timer(!如果你使用 create-react-native-app 你必须弹出它)
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)
在 Android 中为 iOS 和 HeadlessTask 使用后台获取,这是一个不错的库 https://github.com/jamesisaac/react-native-background-task
Use a background-fetch for iOS and HeadlessTask in Android, here is a decent library https://github.com/jamesisaac/react-native-background-task
使用地理位置更新来唤醒应用程序并启动线程https://github.com/mauron85/react-native-background-geolocation.
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.
从服务器推送通知以确保应用程序唤醒应用程序(除非它已被操作系统杀死).在 iOS 中,确保调用 notification.finish()
以避免被任务处理程序算法区分.
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.
对于 Android,您可以尝试使用 AlarmManager API https://github.com/vikeri/react-native-background-job.
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.
更新:Android O 有非常严格的后台执行限制.使用 HeadlessJSTask 服务时,如果您希望它持续超过几秒钟,请确保将其作为前台服务启动.它可能需要通知.请注意,仅加载捆绑包最多可能需要几秒钟,具体取决于您的应用和设备.
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.
这篇关于在 React Native 中安排任务的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!