3
votes

I am planning to build a new offline-first React Native Expo app where the user mainly will be browsing images and text content.

It must be a fully managed Expo app, without ejecting. So Realm DB is not a solution, as it requires ejecting the Expo app

Occasionally, the user will be able to manually trigger a reload of the database (images and JSON files). This will download either the entire database (~50-100MB) or a differential update (which I have no idea on such an implementation yet).

Example of database.json

{
    "dbVersion": "1.0.1",
    "data": {
        "animals": [
            {
                "name": "Dog",
                "age": 5,
                "quantity": 609,
                "images": ["./assets/images/animals/dog/01.jpg", "./assets/images/animals/dog/02.jpg"]
            },
            {
                "name": "Cat",
                "age": 2,
                "quantity": 66,
                "images": ["./assets/images/animals/cat/01.jpg", "./assets/images/animals/cat/02.jpg"]
            }
        ],
        "food": [
            {
                "name": "Banana",
                "color": "Yellow",
                "quantity": 200,
                "images": ["./assets/images/food/banana/01.jpg", "./assets/images/food/banana/02.jpg"]
            }
        ]
    }
}

Question: How will you implement the downloading of the database (JSON file and image files) for such an app? Is there a best practice for such an implementation in React Native w/ Expo?

My current thoughts: Can all the database files (JSON and images) be packaged into a .zip file hosted on a remote server (eg. Amazon S3), and the app will download this single file to its local file storage, delete all files associated with the existing database, then uncompress it the newly downloaded .zip file in its place?

Expo comes with SQLite support. Maybe if images are stored in SQLite as well, Expo can (somehow, unsure how) download a new SQLite db.db from a remote server, and overwrite the existing local copy of db.db?

Or do we have to roll our own converter that takes the downloaded db from the remote server, and convert them to SQL statements, which SQLite will execute to import the downloaded data.

Currently using Expo SDK 32, React Native 0.59.8.

1
How did you solve the problem? I am having the same issuegjfonte
@gjfonte Not solved yet. Hoping for Expo to add support for Realm.Nyxynyx

1 Answers

1
votes

If you want to simple way, use AsyncStorage

//save data
const data = JSON.stringify(yourJson);
AsyncStorage.setItem("@database", data);

//load data
AsyncStorage.getItem("@database").then(dataString => {
  const data = JSON.parse(dataString);
})

or use local database(https://github.com/realm/realm-js)