0

For example I have this simple schema:

class Order: Object {

    @objc dynamic var id = " "

    @objc dynamic var address = " "
    @objc dynamic var callDate = Date(timeIntervalSince1970: 0)

}

I need to make a migration to change the property callDate = Date(timeIntervalSince1970: 0) for an optional type callDate : Date?, I did some test unsuccessfully inside of application(application:didFinishLaunchingWithOptions:).

        let config = Realm.Configuration(
        // Set the new schema version. This must be greater than the previously used
        // version (if you've never set a schema version before, the version is 0).
        schemaVersion: 4,

        // Set the block which will be called automatically when opening a Realm with
        // a schema version lower than the one set above
        migrationBlock: { migration, oldSchemaVersion in
            // We haven’t migrated anything yet, so oldSchemaVersion == 0
            if (oldSchemaVersion < 4) {
                // Nothing to do!
                migration.enumerateObjects(ofType: Order.className()) { oldObject, newObject in
                    // combine name fields into a single field
                    let callDate = oldObject!["callDate"] as! Date?
                    newObject!["callDate"] = callDate
                }
                // Realm will automatically detect new properties and removed properties
                // And will update the schema on disk automatically
            }
        })

    // Tell Realm to use this new configuration object for the default Realm
    Realm.Configuration.defaultConfiguration = config

Output Console Error:

Fatal error: Error initializing database: Error Domain=io.realm Code=10 "Migration is required due to the following errors:
- Property 'Order.callDate' has been made optional." UserInfo={NSLocalizedDescription=Migration is required due to the following errors:
- Property 'Order.callDate' has been made optional., Error Code=10}: file /Users/davidgranado/Documents/projects/Mobile%20Tech2Me/novacopy_ios/MobileTech/Managers/DatabaseManager.swift, line 27

The class that is throw the error:

import Foundation
import RealmSwift

class DatabaseManager {
    enum UpdatePolicy {
        case error, all, modified

        var realmValue: Realm.UpdatePolicy {
            switch self {
            case .error: return .error
            case .all: return .all
            case .modified: return .modified
            }
        }
    }
    // The Error happens here
    static let shared: DatabaseManager = {
        do { return try DatabaseManager() }
        catch { fatalError("Error initializing database: \(error)") }
    }()
}

Any help is welcome, thank you very much!

  • I think you have to update condition like `if (oldSchemaVersion < 4)` – Kishan Bhatiya Feb 16 '20 at 02:37
  • Yeah - you've got a typo here `if (oldSchemaVersion < 1)` which means the migration will only run if the schemaVersion is 0. When moving to a new version, you need to update the schemaVersion *and* that line to match so the code executes. – Jay Feb 16 '20 at 14:10
  • Yes sorry, I updated the `if (oldSchemaVersion < 1)` by `if (oldSchemaVersion < 4)` in the question. It was a typing mistake to write the question here, because in my code is with `if (oldSchemaVersion < 4)` and does not work. – DaviDGJordaN Gj Feb 17 '20 at 13:45
  • Maybe this helps: https://stackoverflow.com/questions/59871974/how-to-change-realm-property-to-nullable/59884071#59884071 – Kishan Bhatiya Feb 17 '20 at 18:23
  • Yes this [answer](https://stackoverflow.com/questions/59871974/how-to-change-realm-property-to-nullable/59884071#59884071) help me, thank you very much @KishanBhatiya !! you are the best! – DaviDGJordaN Gj Feb 17 '20 at 20:04

0 Answers0