react-nativeexporealm

What is the deal with Sync and NonSync in @realm/expo-template?


I'm moving my first steps into react native development and I'm trying to integrate realm for storage, both local and cloud.

I'm going with expo so I downloaded the template from the realm docs. My idea of storage is that, initially the user will be able to use the app without signing up. Then, if he fancies he can create an account to sync the local data to the cloud. This way he can use another device and still have access to his data.

In the template there is AppSync.tsx and AppNonSync.tsx. What does it mean? Do I have to develop different version for local or cloud storage? Also the app decides which one to load based on a flag in sync.config.js. I thought I'd be able to change that at runtime, when the user decides to sign up.

I find the template rather confusing. Can you help me understand?

Edit 1:

I tried switching the flag to load the AppSync and now it requires me to sign in/up straight away (it's not looking "offline first").


Solution

  • Realm and the Realm SDK (now called Atlas Device SDK) is an offline first database, with the ability to Sync to the MongoDB Atlas server, which is 'cloud storage'.

    With sync, Realm data is stored locally first, then automagically sync'd to Atlas later - usually within milliseconds as long as there's an internet connection.

    The two files mentioned in the question demonstrate offline storage only (AppNonSync.tsx), often called "local only" and then sync'd storage (AppSync.tsx). Local only is far simpler to implement and is a great starting point when getting familiar with Realm. For example, this code is all that's needed to configure a local only Realm

    import React from 'react';
    import {RealmProvider} from '@realm/react';
    function AppWrapperLocal() {
      return (
        <RealmProvider schema={[YourObjectModel]}>
          <RestOfApp />
        </RealmProvider>
      );
    }
    

    Sync setup requires more code and is covered in the documentation.

    Anonymous authentication is one way to allow users to try the app, it can later be changed to email/password auth and can be linked to the original data. That's covered in the documentation.

    You do not need to create two different versions; If it's sync, then use the AppSyc template. Noting that even Sync'ing is stored locally so you always have access to your data even when offline. Accessing sync'd data while offline is covered in the Access a Synced Realm While Offline

    One thought is to make your trial version a local only app (which would not require authentication) - and when the user decides they like it when they sign up, their data becomes available on all their devices.