Probleme avec une migration de donnees realm

bonjour,

dans mon code, j’ai ajouté une variable dans ma classe.

j’ai donc mis à jour mon AppDelegate avec le code suivant:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        let config = Realm.Configuration(
            schemaVersion: 2,
            migrationBlock: { migration, oldSchemaVersion in
                if (oldSchemaVersion < 2) {
                }
        })
    
        Realm.Configuration.defaultConfiguration = config
        let realm = try! Realm()
        return true
    }

mais mon application crash au démarrage, et j’ai le message suivant:

thread 1: Fatal error: ‘try!’ expression unexpectedly raised an error: Error Domain=io.realm Code=10

Normalement tu as un erreur un peu plus détaillée dans la console.
Je pense que le chamsp que tu as ajouté est non null, et que donc Realm n’arrive pas a effectuer une migration tout seul.

Il faut donc que tu lui dise quoi faire pour la migration.
Exemple :

            if (oldSchemaVersion < 1) {
                migration.enumerateObjects(ofType: Dog.className()) { oldObject, newObject in
                    // combine name fields into a single field
                    newObject!["id"] = UUID().uuidString
                }
            }

voici l’erreur:

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

j’ai bien essayé ca:

migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 3) {
migration.enumerateObjects(ofType: location.className()) { oldObject, newObject in
newObject!["_devise2"] = “”
}
} }
)

mais rien a faire :frowning:

Es tu sure de la version de la migration ?

Quel est ton schemaVersion ?