Reputation: 83
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 (e.g 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.
Upvotes: 1
Views: 1910
Reputation: 950
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:
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.
Upvotes: 1