20

I am just testing some configurations with Realm and therefore I have added and deleted variables and lists from my realm classes. Since I am just testing I do not want to go through the migration process - I also don't have any data which is effected for continuity.

Is there any way to get around migration being requested automatically by Realm?

kishikawa katsumi
  • 10,418
  • 1
  • 41
  • 53
kangarooChris
  • 718
  • 1
  • 10
  • 21

3 Answers3

37

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 = config
    
    let 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 = config
    
    let realm = try! Realm()
    ...
    
kishikawa katsumi
  • 10,418
  • 1
  • 41
  • 53
  • Great stuff. I have let realm = try! Realm() defined before viewDidLoad. Where can I put your suggested code to make this work before the app crashes because it hits the realm definition? – kangarooChris Jul 05 '16 at 04:02
  • We recommend putting the code on `didFinishLaunchingWithOptions` in AppDelegate. If you put `let realm = try! Realm()` as ViewController's member variable, that is called before `didFinishLaunchingWithOptions`. So it should be `lazy var realm = try! Realm()` – kishikawa katsumi Jul 05 '16 at 04:05
  • I tested both solutions and both work. Great, thanks. – kangarooChris Jul 05 '16 at 04:24
  • @kishikawakatsumi on MacOS I changed to lazy var and reinstalled app, then modified the model adding few fields and run the app. It crashed in viewDidLoad when accessing realm in realm.objects(). I do have auto migration in AppDelegate didFinishLaunchingWithOptions. What else is wrong? Or this solution is only for iOS? Any good example of full project with realm and migration in place? – Vito Valov Jan 27 '18 at 11:29
  • how can you delete the realm from inside the migrationBlock? So you can be selective of what and when data gets deleted. Thanks! – Lucas Chwe Nov 08 '18 at 20:02
9

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 = true
Realm.Configuration.defaultConfiguration = config

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

jaiswal Rajan
  • 4,641
  • 1
  • 27
  • 16
2

Swift 4

var config = Realm.Configuration()
config.deleteRealmIfMigrationNeeded = true
Realm.Configuration.defaultConfiguration = config
Khawar Islam
  • 2,556
  • 2
  • 34
  • 56