Implement a queue for uploading data once network is available

Hello, I am trying to add an iOS native module inside an existing React Native app.

In the native module, I would need to collect step data every hour in the background, and then send the post request to the database. However, if the user doesn’t have a network available, the data should be kept in a queue waiting for the earliest possible opportunity, e.g when the network is active in the next time the phone tries to post.

Would you have any suggestions on how this could be implemented?
Should I persist the data in JSON file writing to phone storage, or use UserDefaults, or Core Data?
Thanks a lot!

In it’s simplest terms your queue is an array, I assume, where you append the most recent to the last position in the array and always pick the first array item to send to the destination and once that is sent, the item at the head of the queue is deleted? or do you keep that data for a longer period of time (as a backup) and keep a current pointer somewhere to keep track of where you are up to in the queue.

What constitutes a step? Is it a data item with more that one property or is it just a timestamp? If you are collecting the data once every hour then using JSON to store in the Document folder would seem to be the simplest option. How long are you anticipating that the device is out of range for?

Is that what you are asking? or are you asking how you would go about having a task in the background doing this work?

Hi Chris, thank you for your reply and also questions to clarify!
My plan is to keep the data in the queue as long as the network is reachable again, then the app should post the data in the queue and clear the queue after that.
I want to implement a background task to handle these processes, with 1-hour interval.
A step data object would consist of a stepCount (Int) and a timestamp (either Date/String). That would also be the request body that we would need to post to the backend.
As we are implementing this as a native module inside an existing React Native app, the task has been challenging so far, at least we still haven’t got the background task run properly yet.

Thank you!

Yeah I’m out of my league when it comes to running background tasks in iOS.

This from Ray Wenderlich might be of assistance.

1 Like

Thank you so much!