Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random Realm 'already opened with a different schema mode' errors

In a Swift app I am setting the default Realm config in didFinishLaunchingWithOptions like this:

Realm.Configuration.defaultConfiguration = {
    var config = Realm.Configuration.defaultConfiguration
    config.deleteRealmIfMigrationNeeded = true
    return config
}()

and then I am creating new Realm instances where needed, in 3 different ways - when reading:

let realm = try! Realm()
let users = realm.objects(User.self)

and when writing:

let realm = try! Realm()
if let user = realm.object(ofType: User.self, forPrimaryKey: userId) {
    try! realm.write {
        user.name = name
    }
}

and in models:

import Foundation
import RealmSwift
import ObjectMapper

final class User: Object, StaticMappable {

    @objc dynamic var id = 0
    @objc dynamic var name = ""

    override static func primaryKey() -> String? {
        return "id"
    }

    func mapping(map: Map) {
        id <- map["id"]
        name <- map["name"]
    }

    static func objectForMapping(map: Map) -> BaseMappable? {
        let objectOptional = try? Realm().object(ofType: self, forMapping: map)
        if let object = objectOptional {
            return object
        }
        return nil
    }
}

The problem is that sometimes I am getting this error when creating new Realm instances in completion closures, which are heavily used:

Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=1 "Realm at path '/var/mobile/Containers/Data/Application/...../Documents/default.realm' already opened with a different schema mode." UserInfo={NSLocalizedDescription=Realm at path '/var/mobile/Containers/Data/Application/...../Documents/default.realm' already opened with a different schema mode., Error Code=1

How can I debug and resolve this?

like image 276
Blackbeard Avatar asked Oct 15 '25 07:10

Blackbeard


2 Answers

I had to make sure that I'm setting Realm.Configuration.defaultConfiguration before I do anything else with Realm. In my case I was first doing: let realm = try! Realm(), and performing some data import tasks, in the appDelegate, before ever setting the default configuration. Then it was failing when being opened from the background thread using the same mechanism. Making sure the default configuration is set before ever calling let realm = try! Realm() does the trick: Now can open on any thread.

like image 138
FranticRock Avatar answered Oct 18 '25 01:10

FranticRock


I was having the same error, in my case, it's because i was using:

<RealmProvider>
   <App />
</RealmProvider>

And exporting a RealmContext in another file like this:

import { createRealmContext } from "@realm/react";

export default createRealmContext({
    schema: [User.schema, Foo.schema, LoremIpsum.schema],
    deleteRealmIfMigrationNeeded: true,
})

I solved removing all above and use only this:

import Realm from "realm";

const refreshUserTable = (values) => {
    try {    
        Realm.open({ schema: [User.schema], deleteRealmIfMigrationNeeded: true })
            .then(realm => {
                realm.write(() => {
                    realm.delete(realm.objects('User'));
                    realm.create('User', values);
                })

                realm.close(); // remember to close
            })

    } catch (error) {
        console.log(error);
    }
}
like image 21
Guilherme Zanetti Avatar answered Oct 18 '25 03:10

Guilherme Zanetti