offline-mobx.mo

Control Points

circle-check

Motivation

  • Your application has to be used in a low connectivity context (abroad, far from towns, in the subway, in Darius's house, ...)

Prerequisites

circle-info

Redux User ?

If you're using Redux, check this out

Steps

Knowing wether or not you're offline (~20min)

circle-check

Create a connectivity store

Reading data offline (~ 20min)

circle-check

Here we will do it by ourselves by using AsyncStorage, you can refer to this MOarrow-up-right to know how to use AsyncStorage

circle-info

Another Solution

You can also use mobx-persistarrow-up-right but in my experience, it's not simpler.

Writing data offline

Defensive (~1h)

circle-check
  • 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.

circle-info

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)

circle-check
  • 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.isConnectedarrow-up-right 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