领域架构版本 0 小于上一个版本 1 Swift

Jus*_*inM 6 realm uitableview ios

我更新了我的数据模型并在应用程序中编写了迁移:didFinishLaunchingWithOptions per Realm 文档:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    let config = Realm.Configuration(

        schemaVersion: 1,


        migrationBlock: { migration, oldSchemaVersion in

            if (oldSchemaVersion < 1) {

            }
    })

           Realm.Configuration.defaultConfiguration = config

        let realm = try! Realm()
Run Code Online (Sandbox Code Playgroud)

我的初始 VC 有一个 tableView 并嵌入到一个导航控制器中,该控制器调用领域,如下所示:

class AllRoundsVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
let realm = try! Realm()
Run Code Online (Sandbox Code Playgroud)

当我这样离开时,我的应用程序在启动时崩溃,显示“致命错误:'尝试!' 表达式意外引发错误:Error Domain=io.realm Code=0 “Provided schema version 0 is less than last set version 1.” UserInfo={NSLocalizedDescription=Provided schema version 0 is less than last set version 1.}

但是,当我在导航控制器前面放置一个虚拟 VC 并带有一个简单的按钮以转到导航控制器时,一切正常。我知道这与 appDidFinishLaunchingWithOptions 在初始 VC 之前或之后加载有关。

我的问题是,如何在调用 try! 的同时停止崩溃!Realm() 在我的初始 VC 上?我不想在 tableView 前面保留一个毫无意义的 VC 只是为了加载正确的模式。

谢谢

Jus*_*inM 1

after doing some realm research on an unrelated issue I came across a solution to this problem. Here is a link to the solution! link

How can I get the migration to run before the app starts to run the code?