Search code examples
realmrealm-mobile-platform

Realm: Can a local realm exist while using a server synced realm?


I have a server synced realm that is working fine. I would like to add an aditional local realm to store some items locally only:

  this.userRealm = new Realm({
    path: 'userRealm.realm',
    schema: [cgps_schema.DirectoryFavoritesSchema],
  });

This does not appear to work. Perhaps its not intended to?

If I call new Realm() before attempting to conect to my synced realm, it creates the userRealm.realm.management directory and the userRealm.realm.lock file, but not the userRealm.realm file. If I call new Realm() after conecting to my synched realm, it creates all the files and works, but when I reload the app it deletes userRealm.realm and creates a new blank one.


Solution

  • You should use different path when you open different realms. Here is some code that opens 1 synced realm and 1 unsynced realm:

    const Realm = require('realm');
    
    const ItemSchema = {name: 'Item', properties: {id: 'int', name: 'string'}};
    
    const unsynced = new Realm({
        path: 'unsynced.realm',
        schema: [ItemSchema],
    })
    
    Realm.Sync.User.register('http://localhost:9080', 'user1', 'pass1', (error, user) => {
        const synced = new Realm({
            path: 'synced.realm',
            schema: [ItemSchema],
            sync: {
                url: 'realm://localhost:9080/~/synced',
                user: user,
            },
        })
        synced.close();
        user.logout();
        unsynced.close();
    })