Realm-js: Realm at path already opened with different schema version.

Created on 12 Mar 2016  路  2Comments  路  Source: realm/realm-js

How to use globally realm database references ? I'm creating db.js like below:

exports.schemas = [KartelaSchema, FirmaSchema, SyncSchema];
exports.version = 11;
let realm = new Realm({schema: [KartelaSchema, FirmaSchema, SyncSchema], schemaVersion: 11});

var db = {
  InsertItems: function(tableName, items){
    realm.write(() => {
      var i, len;
      for(i = 0, len = items.length; i < len; ++i){
        realm.create(tableName, items[i], true);
      }
    });
  },
  DeleteItems: function(tableName){
    let table = realm.objects(tableName);
    realm.write(() => {
       realm.delete(table);
     })
  }
}

module.exports = db;

If I want to use table reference within another component, I'm getting Realm at path already opened with different schema version. error.

forexample here is my home.js :

.....
import DB from '../helpers/db';
let realm = new Realm({schema: DB.schemas, schemaVersion: DB.version});
let kartela = realm.objects('Kartela');
let firma = realm.objects('Firma');
let sync = realm.objects('Sync');
.....

Most helpful comment

That looks like it should work fine. In our example app we export a Realm instance which can be imported in multiple files: https://github.com/realm/realm-js/blob/master/examples/ReactExample/components/realm.js

If you follow this pattern it would prevent you from accidentally using different schema versions in different parts of the app.

If that doesn't resolve things it's possible your issue might be caused by a Realm with an old schema version existing in the cache after you changed your code and refreshed. You can rule this out by quitting the app in the simulator/device and restarting it as this would only occur when refreshing after making changes to the schema version.

All 2 comments

That looks like it should work fine. In our example app we export a Realm instance which can be imported in multiple files: https://github.com/realm/realm-js/blob/master/examples/ReactExample/components/realm.js

If you follow this pattern it would prevent you from accidentally using different schema versions in different parts of the app.

If that doesn't resolve things it's possible your issue might be caused by a Realm with an old schema version existing in the cache after you changed your code and refreshed. You can rule this out by quitting the app in the simulator/device and restarting it as this would only occur when refreshing after making changes to the schema version.

Yes, example app works fine. Thanks!

Was this page helpful?
0 / 5 - 0 ratings