Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run background tasks in React Native?

I've built a little iOS app in 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?

like image 361
liamzebedee Avatar asked Feb 13 '16 06:02

liamzebedee


People also ask

What is HeadlessJS in React Native?

Headless JS is a way to run tasks in JavaScript while your app is in the background. It can be used, for example, to sync fresh data, handle push notifications, or play music.

What is background thread in React Native?

The renderer uses three different threads: UI thread (often called main): The only thread that can manipulate host views. JavaScript thread: This is where React's render phase is executed. Background thread: Thread dedicated to layout.


1 Answers

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.

EDIT 12/2016: There is still no real option. You have the Headless JS API 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.

like image 138
Daniel Schmidt Avatar answered Sep 28 '22 06:09

Daniel Schmidt