Storing objects locally on device and uploading to api on reconnection


#1

Hi all,

I’ve looked at the storage api’s for both react-native and expo and I’m struggling to find a suitable way to structure my app.

I’ve made an app for my workplace in which we scan equipment and record the data. At the moment I’m using firebase to upload the data and store on their servers.

My boss has asked whether we can implement a way of storing the data locally if the user has no access to wifi and then upload the data upon internet reconnection.

I’m not sure where to start with this. I understand that I can use NetInfo and add an event listener to monitor the connection. However I’m not sure how I would persist any scanned data with the opening and closing of the app and such.

Note I can’t detach from expo as I heavily use the expo framework and API within the app.

If anyone could help, it would be greatly appreciated it.

Thanks


#2

@ptgodz

We do offer some offline support, have you checked out:

https://docs.expo.io/versions/latest/guides/offline-support#content

Is your question more about storing data locally on your device when offline?


#3

Hi jimmylee,

Yeah it’s more about how I should handle the storage offline and then a way of uploading to firebase but also removing from offline storage once this happens.

One solution I’ve thought of is to write a method that would use JSON.stringify to save my object to my local storage and use my object id as it’s key
I’d then call another method which would attempt to upload the record to firebase if an active internet connection was found.
If uploaded successfully, i’d update a bool on the object stored locally to define that it’s ready for removal from local storage.
Then I would call a method that parses the JSON data, loops through the objects, and then removes any objects due.

I’d run this whole bunch of code on start up of my app, and then run it everytime a user goes to submit a record.

However I’ve never dealt with async local storage before and I’m not sure whether this is the correct approach but it’s the only thing I could come up with.

If you could point me in the direction of what’s the best way to do this I would really appreciate it.


#4

Have you looked into firebase’s built in offline persistence: https://firebase.google.com/docs/database/web/offline-capabilities


#5

Hi. Firebase does this for you. Do the test. Disconnect the data from your cell phone and make your application work. You will see that when you restore the data in the cellphone, the firebase will connect to the server and send the data automatically.
I’m using that, because where they use my applications are usually places where they lack a good data signal or wifi and it works very well.
Greetings.


#6

@bacon @atiladev

Thanks for your comments. I’ve looked into firebase’s offline persistence capabilities and initially thought it would be an easier way to go about this.

However, I’m building an app that people will be using offshore and there is a possibility that users might not have an internet connection for weeks at a time so I’m not sure whether firestore offline would be suitable.

Is there any other way around this? Thanks


#7

I’ve spoken to my boss and it looks like this might be the better option.

Could you provide any materials for me to read that reference using this?

I’ve read online that is enabled by default on mobiles using native code, however the web SDK requires enabling.

I’m assuming it’s not just as simple as

firebase.initializeApp(config);
firebase.firestore().enablePersistence();

Or do I have to enable persistence on every call I make to firestore?

Thanks again


#8

I think you could just watch the connectivity then enable persistence based on that.

const ref = firebase.database().ref(".info/connected");
ref.on("value", snap => {
   if (snap.val() === true) {
      //"connected"
   } else {
      //"not connected"
   }
});

closed #9

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.