Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Realm at path '/var/.../default.realm' already opened on current thread with different schema

Tags:

realm

Getting this error when attempting to do Realm.open({schema: [Schema]}) on a new build/install on an iOS device/simulator. Everything works fine on the Android application as it has never changed schemaVersions with a migration before.

My understanding of this error is that the schemaVersion of the Realm file on the device is different from the default schemaVersion: 0 of the Realm.open({schema: [Schema]}). Another theory that I have is that my application is attempting to open a Realm configuration during an existing opened Realm configuration on the same file.

In order to address my first theory, I have been trying to reset the realm file with Realm.clearTestState() and Realm.deleteFile({schema: [Schema]}) to no avail. For the second, I've placed concurrent Realm.open() instances in setTimeout() so that only one Realm instance is open at a time.

Is there any other method I can try to reset the Realm file so that both the Realm file on device and the configuration are both the same schemas?

like image 510
Friendly-Robot Avatar asked Feb 03 '18 23:02

Friendly-Robot


1 Answers

It turns out that I was not opening the Realm with the exact same schema in every instance. Meaning I was exporting multiple object schemas from my realm.js file and was opening them individually rather than as a whole, which I suppose is the correct method.

So basically, I was doing this...

Realm.open({schema: [object1, object2, object3]})
Realm.open({schema: [object4, object5]})

when I should've been doing this:

const Schema = [object1, object2, object3, object4, object5];
Realm.open({schema: Schema});

Hope this helps anyone else running into this issue as I had visited every other similar error report on Github and StackOverflow and had not come across this tip.

like image 98
Friendly-Robot Avatar answered Oct 22 '22 19:10

Friendly-Robot