How can I avoid migration in RealmSwift How can I avoid migration in RealmSwift swift swift

How can I avoid migration in RealmSwift


There are two ways to skip migration error regardless schema changes.

  1. Use deleteRealmIfMigrationNeeded property. If it is true, recreate the Realm file with the provided schema if a migration is required.

    let config = Realm.Configuration(deleteRealmIfMigrationNeeded: true)Realm.Configuration.defaultConfiguration = configlet realm = try! Realm()...

      

  2. Increment schema version every launch. Realm has auto migration feature. If you don't need to migrate existing data, you can just increment schema version. Schema will be changed by Realm automatically.

    let config = Realm.Configuration(schemaVersion: try! schemaVersionAtURL(Realm.Configuration.defaultConfiguration.fileURL!) + 1)Realm.Configuration.defaultConfiguration = configlet realm = try! Realm()...


In Swift 3

Migration in Realm can be easily avoid by placing this code inside "didFinishLaunchingWithOptions" method in AppDelegate class.

var config = Realm.Configuration()config.deleteRealmIfMigrationNeeded = trueRealm.Configuration.defaultConfiguration = config

This will delete the realm database if migration is required with new setup.


Swift 4

var config = Realm.Configuration()config.deleteRealmIfMigrationNeeded = trueRealm.Configuration.defaultConfiguration = config