简体   繁体   中英

React native schedule a job

I am new to react-native, i am developing an application in which i have to sync data to the server. The idea is to sync the data after certain period of time (eg minutes or hours) when the app is in front/background .

to achieve this, do i have use react native timers function such

setInterval

to keep syncing data to the server while app is open and to the sync data in background do i have to use

Headless JS .

I am not sure what approach should I use. I am open to suggestions or what are the best practices to sync data to the server after certain period of time.

Setting up a recurring background task that executes periodically even when app is closed is pretty easy to do in React Native now.

Integrate react native background task to actually schedule your function with your syncing logic that gets fired in the background periodically.

Caveats of cross platform background tasks:

  • Minimum interval to execute the task in background is ~15 min.
  • Task execution timeout limit is 30 seconds.
  • Task is not guaranteed to execute exactly at interval specified. iOS/Android uses a variety of signals to determine if task execution should be delayed (battery life/current cpu usage/etc).

If you have more advanced tasks you need to process in the background, integrate react native queue into your app for job management. The queue will handle all the logic dealing with the hard 30 second timeout limit set by the OS for you (ctrl+f "queue lifespan") and it will persist your tasks across device restarts as well as handle retry-on-failure, individual task timeouts, etc, for you.

I've written a detailed tutorial on accomplishing this here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM