Realm - removal of objects from an area in a migration block

I need to remove objects from scope during migration.

I have an AccountManager that contains:

func logOut() { let realm = try! Realm() try! realm.write { realm.delete(realm.objects(Account.self)) realm.delete(realm.objects(Address.self)) ... // Other deletions } } 

But whenever I use the logOut () function in a migration block, it just fails.

  let config = Realm.Configuration( schemaVersion: 11, migrationBlock: { migration, oldSchemaVersion in if (oldSchemaVersion < 11) { // Delete objects from realm AccountManager().logOut() // DOESN'T WORK } }) Realm.Configuration.defaultConfiguration = config 

I need users to reboot after this update. Is there any way to perform these deletions in the migration block?

+5
source share
2 answers

You can tell Realm to remove when reconfiguration is required.

 Realm.Configuration.defaultConfiguration = Realm.Configuration( schemaVersion: 10, migrationBlock: { migration, oldSchemaVersion in }, deleteRealmIfMigrationNeeded: true ) 
+5
source

You can use Migration.deleteData(forType typeName: String) instead of Realm.delete(_:) as follows.

 Realm.Configuration(schemaVersion: 11, migrationBlock: { migration, oldSchemaVersion in if oldSchemaVersion < 11 migration.deleteData(forType: Account.className) migration.deleteData(forType: Address.className) ... 
+4
source

Source: https://habr.com/ru/post/1259160/


All Articles