Realm Migration doesn't work

后端 未结 8 1184
天涯浪人
天涯浪人 2020-12-28 17:16
    let config = Realm.Configuration(
        // Set the new schema version. This must be greater than the previously used
        // version (if you\'ve never set a         


        
8条回答
  •  粉色の甜心
    2020-12-28 17:50

    I also had this issue happening where my app would crash despite the fact that I added the default migration code in didFinishLaunchingWithOptions

    As already stated here, the problem was that I was initializing an instance of Realm in my first view controller as a class level property.

    BUT I couldn't just remove this instance and put it in the viewDidLoad, as I needed to use it in several functions.

    The solution was actually to add the 'lazy' keyword, so the migration can be done before the initialization, as I found over here: https://www.selmanalpdundar.com/solution-of-realm-migration-error-code-10.html

    import UIKit
    import RealmSwift
    
    class ViewController: UIViewController {
        lazy var realm = try! Realm() //added lazy and changed let to var
    }
    

提交回复
热议问题