Javascript 如何在 React Native 中运行后台任务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35376690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I run background tasks in React Native?
提问by liamzebedee
I've built a little iOS appin React Native that does location tracking, sending the lat/lng regularly to a server of the user's choosing. However this only works when the app is in the foreground. How can I run this task in the background when the user is in other apps?
我在 React Native 中构建了一个小的 iOS 应用程序,它进行位置跟踪,定期将 lat/lng 发送到用户选择的服务器。但是,这仅在应用程序处于前台时才有效。当用户在其他应用程序中时,如何在后台运行此任务?
采纳答案by Daniel Schmidt
Currently, there is, unfortunately, no support for background tasks of any kind. The feature you are referring to would be a background timer. Such a timer is this product pain (a feature request)for react native, you may upvote it to show an increased demand for this feature.
不幸的是,目前不支持任何类型的后台任务。您所指的功能是后台计时器。这样的计时器是React Native 的产品痛点(功能请求),您可以对其进行投票以显示对该功能的需求增加。
EDIT 12/2016:There is still no real option. You have the Headless JSAPI since RN 0.33, but it is only for android. Also your App will crash if this runs in the foreground so you have to be careful about using it. Thanks to @Feng for pointing that out.
编辑 12/2016:仍然没有真正的选择。自 RN 0.33 起,您就有Headless JSAPI,但它仅适用于 android。如果它在前台运行,您的应用程序也会崩溃,因此您必须小心使用它。感谢@Feng 指出这一点。
回答by Matthew Wilcoxson
And now there is react-native-background-taskwhich is a single API for both Android and iOS.
现在有了react-native-background-task,它是 Android 和 iOS 的单一 API。
回答by frontendloader
The React Native ecosystem has been moving at breakneck pace over the past few months, and a few plugins have popped up to address the pain of not being able to run code in the background.
在过去的几个月里,React Native 生态系统一直在以惊人的速度发展,并且出现了一些插件来解决无法在后台运行代码的痛苦。
https://github.com/transistorsoft/react-native-background-fetch-- Wake up for 30 seconds periodically to run some arbitrary JS code. Not suitable for high resolution geolocation, as the time between wake-ups will be 15min or more.
https://github.com/transistorsoft/react-native-background-fetch-- 定期唤醒 30 秒以运行一些任意 JS 代码。不适合高分辨率地理定位,因为唤醒之间的时间将是 15 分钟或更长时间。
https://github.com/transistorsoft/react-native-background-geolocation-- A better fit for this situation, targeted specifically at geolocation in the background.
https://github.com/transistorsoft/react-native-background-geolocation——更适合这种情况,专门针对后台的地理定位。
回答by Provash Shoumma
These libraries can help you to achieve your desired functionality:
这些库可以帮助您实现所需的功能:
Alternatively, you can use Headless JSfrom react-native. But its only available for Android.
或者,您可以使用react-native 中的Headless JS。但它仅适用于Android。
回答by Venryx
I use this, and it seems to work: https://github.com/ocetnik/react-native-background-timer
我使用这个,它似乎工作:https: //github.com/ocetnik/react-native-background-timer
Emit event periodically (even when app is in the background).
You can use the setInterval and setTimeout functions. This API is identical to that of react-native and can be used to quickly replace existing timers with background timers.
定期发出事件(即使应用程序在后台)。
您可以使用 setInterval 和 setTimeout 函数。此 API 与 react-native 相同,可用于将现有计时器快速替换为后台计时器。
import BackgroundTimer from 'react-native-background-timer';
// Start a timer that runs continuous after X milliseconds
const intervalId = BackgroundTimer.setInterval(() => {
// this will be executed every 200 ms
// even when app is the the background
console.log('tic');
}, 200);
// Cancel the timer when you are done with it
BackgroundTimer.clearInterval(intervalId);
// Start a timer that runs once after X milliseconds
const timeoutId = BackgroundTimer.setTimeout(() => {
// this will be executed once after 10 seconds
// even when app is the the background
console.log('tac');
}, 10000);
// Cancel the timeout if necessary
BackgroundTimer.clearTimeout(timeoutId);