Realm migration not called

匿名 (未验证) 提交于 2019-12-03 01:38:01

问题:

I've added a value to a realm object (I've added dynamic var inspectorName = "" to the WeekReport object), and I'm trying to migrate the realm database to contain that value. I'm trying to call the migration block in func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) like this:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {     print("HERE")     Realm.Configuration.defaultConfiguration = Realm.Configuration(         schemaVersion: 1,         migrationBlock: { migration, oldSchemaVersion in             if (oldSchemaVersion < 1) {                 migration.enumerateObjects(ofType: WeekReport.className()) { oldObject, newObject in                     newObject!["inspectorName"] = ""                 }             }     })      return true } 

But it seems that didFinishLaunchingWithOptions isn't called before my error happens.

In multiple view controller i have let realm = try! Realm(). Here Xcode breaks when I run the app:

"Migration is required due to the following errors: - Property 'WeekReport.inspectorName' has been added." UserInfo={NSLocalizedDescription=Migration is required due to the following errors: - Property 'WeekReport.inspectorName' has been added., Error Code=10}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.63/src/swift/

How come the migration blick isn't called? "HERE" is never printed...

Should I define realm in a different way in my view controllers?

回答1:

If you write let realm = try! Realm() in a view controller as an instance variable, it will be called before application: didFinishLaunchingWithOptions from Storyboard. To resolve this, you can use lazy var realm = try! Realm() instead. lazy defers creating an instance variable until the variable is accessed.



回答2:

In my case schemaVersion: 1, was to low and migration block was never called. Make sure that your new version is greater then previous.

It was my first migration but I had to change it to schemaVersion: 2 and then it started work.



回答3:

In multiple view controller i have let realm = try! Realm().

It seems like one of your view controllers creates Realm before application: didFinishLaunchingWithOptions, so the default configuration with migration is not set by that time.

Make sure that you configure Realm.Configuration.defaultConfiguration before any instances of Realm are created.



回答4:

In your code, try calling:

 _ = try! Realm() 

before:

return true 

It worked for me!



回答5:

It seems the problem is the migration is an async call and we can only access the Realm after the migration is completed. So the flow should be first: Set the configuration. Second: Use Realm.asyncOpen to force Realm update and hold off other things which may access try! Realm(). You can find more details here: https://realm.io/docs/swift/latest/#asynchronously-opening-realms



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!