Realm migration issue. How do I Update the data that should be in it
Asked Answered
A

2

6

I copy and paste the code from realm doc. But don't know how to change teh section i commented next to to indicate. (at the bottom) Below is the full error message I get:

error initializing newrealm, Error Domain=io.realm Code=10 "Migration is required due to the following errors: - Property 'Item.dateCreated' has been added." UserInfo={NSLocalizedDescription=Migration is required due to the following errors: - Property 'Item.dateCreated' has been added., Error Code=10} 2018-07-30 21:25:24.231575-0400 Todoey[87561:3063712] *** Terminating app due to uncaught exception 'RLMException', reason: 'Invalid property name 'dateCreated' for class 'Category'.'

Below is the code in witch I attempted the migration:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    print(Realm.Configuration.defaultConfiguration.fileURL)

    do {
        let realm = try Realm()
    } catch {
        print("error initializing newrealm, \(error)")
    }

    //Migration
    let config = Realm.Configuration(

        schemaVersion: 1,

        migrationBlock: { migration, oldSchemaVersion in

            if (oldSchemaVersion < 1) {

                migration.enumerateObjects(ofType: Category.className()) { (old, new) in
                    new!["dateCreated"] = Date()
                }
                migration.enumerateObjects(ofType: Item.className()) { (old, new) in
                    new!["dateCreated"] = Date()
                }
            }
    })

    Realm.Configuration.defaultConfiguration = config
    //Migration X

    return true
}

It appears that the problem is above where the "// combine name fields into a single field" comment is. I need to change those values to the following:

class Item: Object {
    @objc dynamic var title: String = ""
    @objc dynamic var done: Bool = false
    @objc dynamic var dateCreated = NSDate() //this is the new data
    var parentCategory = LinkingObjects(fromType: Category.self, property: "items")
}
Ackack answered 30/7, 2018 at 23:15 Comment(1)
You can find my solution in this answer: https://mcmap.net/q/576443/-realm-migration-doesn-39-t-workSunk
A
7

The problem is the new property which has been added to realm database.

When you run your app in simulator, it will pull up old realm database without this new property.

Solution working in Xcode 10, Swift 4.2:

  • Run simulator
  • Press and hold your app icon and when it jiggles, just delete it from simulator.
  • Build and run app again from Xcode and simulator will pull up fresh realm database with new property added. This will delete all your data from the old realm database and start fresh.

NOTE: This solution is good when app is still in development and using simulators. If your app is already published and in production, then u should increment schema version of database. There is guide on Realm website how to do that.

Hope this helps.

Aldwin answered 7/11, 2018 at 15:21 Comment(0)
C
0

In your case - since you merely added a new property - all you need to do is increment the schema version to 2, and Realm will take care of the rest.

Carthusian answered 31/7, 2018 at 0:22 Comment(4)
This might sound stupid but to increment shoudl I change the 1 to 2 everywhere where they rfer to the schema version?Ackack
The migration should only be done in one spot. You've got the example they provided duplicated there. You only need one of those. Just supply it in the schemaVersion parameter where it currently says schemaVersion: 1. Every time you make a change to your Realm objects, you need to increment that value. The example they provide with the first name and last name shows you how you would handle changes to existing properties and migrate existing data over to the new schema.Carthusian
I updated code as seen above and I did increment it by 1 from. But now I get the following:error initializing newrealm, Error Domain=io.realm Code=10 "Migration is required due to the following errors: - Property 'Item.dateCreated' has been added." UserInfo={NSLocalizedDescription=Migration is required due to the following errors: - Property 'Item.dateCreated' has been added., Error Code=10} 2018-07-30 21:25:24.231575-0400 Todoey[87561:3063712] *** Terminating app due to uncaught exception 'RLMException', reason: 'Invalid property name 'dateCreated' for class 'Category'.'Ackack
You also need to set the configuration before initialising the Realm. Do it before the "let realm = try Realm()".Carthusian

© 2022 - 2024 — McMap. All rights reserved.