0

There's a way for Android to achieve this but I still puzzled on iOS approached. I don't want to do migration every time I have new update so I prefer clear db to start over if it is so.

I found this question related to this

I can only use this code for migration?

// Notice setSchemaVersion is set to 1, this is always set manually. It must be
// higher than the previous version (oldSchemaVersion) or an RLMException is thrown
[RLMRealm setSchemaVersion:1
        forRealmAtPath:[RLMRealm defaultRealmPath]
    withMigrationBlock:^(RLMMigration *migration, uint64_t oldSchemaVersion) {
   // We haven’t migrated anything yet, so oldSchemaVersion == 0
    if (oldSchemaVersion < 1) {
    // Nothing to do!
    // Realm will automatically detect new properties and removed properties
    // And will update the schema on disk automatically
  }
}];
Community
  • 1
  • 1
shoujo_sm
  • 3,173
  • 4
  • 37
  • 60

1 Answers1

0

Yes, you can only use this for migration. From the official docs:

You define a migration and the associated schema version by calling +[RLMRealm setSchemaVersion:forRealmAtPath:withMigrationBlock:]. Your migration block provides all the logic for converting data models from previous schemas to the new schema. After calling +[RLMRealm setSchemaVersion:forRealmAtPath:withMigrationBlock:], any Realms which require migration will automatically apply the migration block provided and be updated to the provided version.

Read more about migrations

You could try however to delete a Realm and to create a new Realm in a new path instead. So you would keep a version number in UserDefaults and update that number every time your data model changes. I don't know if that works though. From the Docs:

You can also set a custom path for the default realm using +[RLMRealm setDefaultRealmPath:]. This is especially useful when testing your app, or when sharing realms between apps in an iOS 8 Shared Container.

Read more about Realm

ezcoding
  • 2,974
  • 3
  • 23
  • 32