offline-mobx.mo
Owner: Maxime Sraïki
Control Points
If you want to make your application work offline, you'll need to check that
Motivation
Your application has to be used in a low connectivity context (abroad, far from towns, in the subway, in Darius's house, ...)
Prerequisites
Redux User ?
If you're using Redux, check this out
Steps
Knowing wether or not you're offline (~20min)
Control points
Create a connectivity store
Reading data offline (~ 20min)
Control points
Here we will do it by ourselves by using AsyncStorage
, you can refer to this MO to know how to use AsyncStorage
Another Solution
You can also use mobx-persist
but in my experience, it's not simpler.
Writing data offline
Defensive (~1h)
Control points
Define what call you want to make work offline
Store the payload to your application state as pending
Persist it
Whenever there is something stored as pending in your store, display a warning message to the user to let him know
When he wants to, let him do the call again with the stored payload
Clear the pending observable when your call is succesful
In your offline store:
In your component
We implemented a working example of it @BAM, let me know if you want to know more about it.
Go further
This is a working example for a one shot call but you can also write a queue of calls letting you store multiple payloads with multiple calls.
Optimistic (~2h00)
Control points
Define what call you want to make work offline
Store the payload to your application state as pending
Persist it
Whenever there is something stored as pending in your store display a discret message/icon to the user to let him know
When you get online again (you can use your connectivity observable) try to do the call again
Define a fail detection strategy (for instance after n tries, decide that the called is failed and rollback the state modification you made)
Tips & TroubleShoot
In order to be able to detect this, you can use the react-native's
NetInfo.isConnected
function to get the connectivity status your phone thinks it has.There are ways to check user's connectivity by pinging one of your own server routes instead of basing it on the phone self awareness of its connectivity.
We did not implement offline calls that would need conflict management (several users able to access the same data at the same time) yet.
Last updated