3

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.

Nyxynyx
  • 61,411
  • 155
  • 482
  • 830

1 Answers1

1

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)

강현구
  • 33
  • 4
  • Do you recommend storing binaries, especially images (<1 MB each) in Realm? If so, I think this makes it easier to handle images. – Nyxynyx Aug 22 '19 at 17:38
  • [Realm does not appear to be supported by Expo](https://expo.canny.io/feature-requests/p/support-for-realm). Unfortunately I will require the app to be a fully managed Expo app, without ejecting it. – Nyxynyx Aug 22 '19 at 17:45
  • @Nyxynyx refer this page when use Expo(https://docs.expo.io/versions/latest/sdk/sqlite/) I recommend just store images into device(like cache files) – 강현구 Aug 23 '19 at 01:19
  • 1
    Thanks. Do you know if it is possible to download a SQLite db file to device using Expo iOS app and replace the old db file with the newly downloaded db file? I am thinking of using this method to update the db completely – Nyxynyx Aug 23 '19 at 04:03