Easy way to convert a Local Realm to Realm Cloud


#1

Hi, i have an application that use Local Realm today. Now, i need sync this data to a realm cloud instance and retrieve this data back when needed. Is there an easy way to do this conversion?

Thanks in advance,

Robson Barreto


#2

Yes,

you only need to copy your exist data to a synced realm. The framework will do the job for you :slight_smile:


#3

I took a look at the doc, and actually is really easy to implement. Thanks! :slight_smile:


#4

No problem :slight_smile:
Feel free to ping me if you have any question.


#5

Just one more question: If i have previous realm data locally (in production) this data will sync automatically or i need to do some routine to migrate this old realm data to cloud?


#6

This is what I meant as copy old data.

Any data stored in a local realm wont sync, but it will if you copy it to a synced realm.
The only difference between a synced and a not synced realm is the configuration that you use to instantiate.


#7

Any light about how can i do that?


#8

Just open both of the realms, and copy the local object to the synced realm.


#9

I didn’t found any example in Java showing how to do this migration, so i wrote my own migration.
If someone with the same problem find this thread, this algorithm should work fine:

private void copyLocalRealmToSyncedRealm(@NonNull final MigrationListener migrationListener) {
        RealmConfiguration localConfig = Realm.getDefaultConfiguration();
        RealmConfiguration syncConfig = orderQuery.getSyncConfig();
        if (localConfig != null && syncConfig != null) {
            final Realm syncedRealm = Realm.getInstance(syncConfig);
            final Realm localRealm = Realm.getDefaultInstance();
            final Set<Class<? extends RealmModel>> classes = localConfig.getRealmObjectClasses();
                syncedRealm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(@NonNull Realm realm) {
                        try {
                            if (classes.size() > 0) {
                                for (final Class realmClass : classes) {
                                    final RealmResults<? extends RealmModel> realmModels = localRealm.where(realmClass).findAll();                               
                                    realm.insertOrUpdate(realmModels);                                 
                                }
                            }
                            migrationListener.done();
                            navigateToNextScreen();
                        } catch (Exception e) {
                            if (SyncUser.current() != null) {
                                SyncUser.current().logOut();
                                navigateToNextScreen();
                            }
                        }
                    }
                });
            }
    }

PS 1: MigrationListener is just an interface that notify when migration is done. I use that to know when i’m ready to delete my old local realm.

PS 2: “orderQuery.getSyncConfig()” just encapsulate SyncConfiguration creation, you could create this inside this same method or get it from other source as well.


#10

I write a dynamic programming solution for java scripting easier to developers and recently connect with experts of online dissertation help research, theorem vary different approaches in order to correct mathematical abstraction programming solutions of graph-theory applied.