Hi I am trying to figure out how to loop through client side storage collection, and then for each record post them to an api url.
i am trying to create a guaranteed message delivery loop. (basically if an api is not available the first time i try send data to it, add the raw message to an outgoing queue and send later when its back up or i am in cell tower range again.)
I’m not sure if we’ve tried this kind of looping before. Did you try with a single record if you managed to send a XMLHttpRequest properly, that you have the configuration and everything right?
I can’t comment much on the code itself as me and JS aren’t great pals, but the way we do offline support is by saving whatever data is needed into a sync queue on the device, a local resource, and then sync from there periodically, e.g. when a “sync” button is pressed, or when app is opened or something. This can be done by used readily made flow functions, so that’s what I would recommend, if that’s possible for you. That would require having a rest api endpoint where you could post the records.
this is proving very clunky for a data and api perspective, not all apis are for just CRUD.
I haven’t worked on this myself so I can’t unfortunately explain to you with great confidence, but I’ll tell you what my understanding of how this has been done in our apps is.
First of all, the data is stored in the app in three places. There is the API end-point data, which is fetched from and which is synced to when the app has network connection (and the user wants to sync, or the app has determined that it should sync now). There is the data stored in local storage which is where the data is stored from the API end-point, to where the user’s changes are saved, and from which the sync to the API end-point data is made. And then there is the app variable data, which is where the data is stored to be displayed in UI elements, which is modified according to user’s changes, and from which the changes to the locally stored data are made immediately, so as to not lose changes. The locally stored data is stored via the set/get storage flow functions.
The important thing is the comparison of the API end-point data to the data stored in local storage. There may be conflicts in these if the user is offline and changes were made to the backend from somewhere else, and you have to solve these in the app.