Michal
Michal

Reputation: 15669

How to migrate Realm object that hasn't been stored?

I have an object that I haven't stored anywhere (yet). Even without creating a Realm for it, the app crashes saying this object requires a migration after I've modified it.

I've tried this (in method func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool):

RLMRealm.setSchemaVersion(1, forRealmAtPath: RLMRealm.defaultRealmPath()) { (migration: RLMMigration!, oldSchemaVersion: UInt) -> Void in
    if oldSchemaVersion < 1 {
        // not needed, nothing stored...
    }
}
RLMRealm.defaultRealm()

Log:

*** Terminating app due to uncaught exception 'RLMException', reason: 'Migration is required for object type 'PYDRealmChange' due to the following errors:
- Property 'relativePath' is missing from latest object model.
- Property 'source' has been added to latest object model.
- Property 'target' has been added to latest object model.'

which is what I've found in the documentation. It didn't help and the app is still crashing. My guess is that I need the actual Realm to perform the migration on. So my question is - how do I migrate if there is none?

Upvotes: 0

Views: 1258

Answers (1)

Michal
Michal

Reputation: 15669

So problem solved:

What I didn't know is the fact, that I guess that the model object is set in all the realms existing in the app no matter whether I have stored it in them or not. The solution was to copy/paste the code RLMRealm.setSchemaVersion... for all the realm objects in my app. Then it started working.

Upvotes: 1

Related Questions