0
votes

testing realm, cannot add a new property to my testing class. Followed documentation and some answer here but both are not working. In my code even the "plain" empty default block for migration is not working, using updated realm.

getting this error

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 'ComicType.creationDate' has been added." UserInfo={NSLocalizedDescription=Migration is required due to the following errors: - Property 'ComicType.creationDate' has been added., Error Code=10}

it seems I'm not changing the schemaVersion, but I did in the "basic" migration block. I check the schema version in app delegate with:

 let configCheck = Realm.Configuration();
        do {
            let fileUrlIs = try schemaVersionAtURL(configCheck.fileURL!)
            print("schema version \(fileUrlIs)")
        } catch  {
            print(error)
        }

my AppDelegate:

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.


        let config = Realm.Configuration(schemaVersion: 1, migrationBlock: { migration, oldSchemaVersion in

                if (oldSchemaVersion < 1) {
                }

        })

        Realm.Configuration.defaultConfiguration = config

        _ = try! Realm()



        return true
    }

my class:

import Foundation
import RealmSwift

class ComicType: Object {
    @objc dynamic var name : String = ""
    @objc dynamic var creationDate : Date? //the new property

}
2
Please specify "not working". You don't need to do anything in your migration block, Realm can handle a simple migration like this (adding a new property with a default value), so there must be something else going on if an empty migration block didn't work. - Dávid Pásztor
added the error message - biggreentree
Are you sure your schemaVersion just needs to be 1? Is this your first migration? - Dávid Pásztor
yes, it is a test app, everything went right editing objects, but testing migration scripts I cannot get rid of this error. - biggreentree
As a test, I copied and pasted your code into a project, with the ComicType original creationDate property. In code I added a few objects. I then commented out the old CreationDate and uncommented the new one, changed the schemaVersion to 2 and ran the code. It worked fine. I then made a number of changes (adding & removing properties etc) to the ComicType object, updating the migration number each time and it worked correctly. Also, what are you expecting by changing an optional var Date? = nil to another optional var creationDate : Date?. That change doesn't require a migration. - Jay

2 Answers

3
votes

Issue solved this way: the real issue is not about schema number, but about not calling at all the migration code, I realized the migration schema remained always the same. True error is calling let realm = try! Realm() in my view controller using it as global constant. Now using lazy var realm = try! Realm() Great help found here . Now after each change in my model, I update in appDelegate the schemaVersion.

1
votes

What worked for was to remove the app from the iOS Simulator, and then run it again using XCode. More about this is found here: https://github.com/RocketChat/Rocket.Chat.iOS/issues/104

I was facing a similar issue, my error stack trace was beginning like this:

`fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=10 "Migration is required due to the following errors:

The errors shown was just confirming the some objects have been saved locally. so, there was no clue on what caused the problem.

What I figured was, the issue was that the schema of Realm Database was violated, since I updated the Objects' classes that I have been saving locally. So, it requested that I update the schema.

I suppose that a new Realm file with is created when the app is removed and run again. If so, then a new schema is created that works with latest code