Migration error code 10, although schema has not been altered


#1

I’m trying to query from the csv inside the default.realm which I have added into the app’s bundle. That .realm file has been added to “Copy Bundle Resources” under “Build Phases” already.

First I didn’t have the thing with the schemaVersion and the migration code inside the configuration of the Realm. Even after I added it into it, it didn’t help at all. I still get the same error saying that FragenExp.ID apparently has been altered/deleted, which it hasn’t been. I also have deleted the app from the Simulator, but that didn’t help either.

Perhaps I didn’t use the migrationBlock and schemaVersion correctly? I just pasted it in there and Xcode didn’t give me any errors on that.

Error message:
Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=10 "Migration is required due to the following errors: - Property 'FragenExp.ID' has been removed." UserInfo={NSLocalizedDescription=Migration is required due to the following errors: - Property 'FragenExp.ID' has been removed., Error Code=10}

I get that error when opening the ViewController with the following viewDidLoad() code:

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let config = Realm.Configuration(fileURL: Bundle.main.url(forResource: "default", withExtension: "realm"), schemaVersion:0,
                                         migrationBlock: { migration, oldSchemaVersion in
                                            // We haven’t migrated anything yet, so oldSchemaVersion == 0
                                            if (oldSchemaVersion < 1) {
                                                // Nothing to do!
                                                // Realm will automatically detect new properties and removed properties
                                                // And will update the schema on disk automatically
                                            }
        })
        
        
        let realm = try! Realm(configuration: config)
        let results = realm.objects(FragenExp.self)
        print(results[0])
    }

The class FragenExp is declared as following:

import Foundation
import RealmSwift

class FragenExp:Object {
    var ID:Int?
    var Fragen:String?
    var r0:String?
    var f1:String?
    var f2:String?
    var f3:String?
}

Here is a picture of the .realm opened in Realm Studio

Thanks in advance!!
Kevin


#2

Maybe it’s not related to the issue, but is it a good idea store an ID as an optional [i assume its the key property]?