如何等到 Realm.io 迁移完成?

How to wait until Realm.io Migration finish?

我在 Appdelegate 中进行迁移,但我也有逻辑显示哪个导航控制器基于 Realm 中的 UserObject。

[RLMRealm setSchemaVersion:3
            forRealmAtPath:[RLMRealm defaultRealmPath]
        withMigrationBlock:^(RLMMigration *migration, NSUInteger oldSchemaVersion) {

            [migration enumerateObjects:App.className
                                  block:^(RLMObject *oldObject, RLMObject *newObject) {                                 
                                      if (oldSchemaVersion < 3) {
                                          newObject[@"watchedTutorial"] = false;
                                      }
                                  }];
        }];


if([[UserManager sharedInstance] isUserLoggedIn]){

    UINavigationController *navController = [MAIN_STORYBOARD instantiateViewControllerWithIdentifier:@"BookingNavController"];
    self.window.rootViewController = navController;
    self.navController = navController;

}else{

    UINavigationController *navController = [MAIN_STORYBOARD instantiateViewControllerWithIdentifier:@"NavController"];
    self.window.rootViewController = navController;
    self.navController = navController;

}

应用程序崩溃,因为在后台完成迁移之前访问 [[UserManager sharedInstance] isUserLoggedIn]。我应该怎么做才能克服这个问题?

谢谢

更新: 这是UserManager代码供参考

class UserManager: NSObject{

    // Singleton    
    class var sharedInstance: UserManager {
        struct Static {
            static var instance: UserManager?
            static var token: dispatch_once_t = 0
        }

        dispatch_once(&Static.token) {
            Static.instance = UserManager()
        }

        return Static.instance!
    }

    // Vars
    var realm = RLMRealm.defaultRealm()
    var currentUser:User?

    // Class Methods
    func getCurrentUser() -> (User){

        let result = User.allObjects();
        if result.count > 0 {
            currentUser = result[0] as? User

        }else{
            let obj = User()
            realm.beginWriteTransaction()
            realm.addObject(obj)
            realm.commitWriteTransaction()
            currentUser = obj
        }

        return currentUser!
    }

    func isUserLoggedIn() -> (Bool){
        return AppManager.sharedInstance.isLoggedInAsGuess()
    }
}

异常

*** Terminating app due to uncaught exception 'RLMException', reason: 'Migration is required for object type 'App' due to the following errors:
- Property 'watchedTutorial' has been added to latest object model.'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010edb2f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010ea4abb7 objc_exception_throw + 45
    2   MyApp                            0x000000010b6c7edb _Z24RLMVerifyAndAlignColumnsP15RLMObjectSchemaS0_ + 5707
    3   MyApp                            0x000000010b6c668b RLMRealmSetSchema + 875
    4   MyApp                            0x000000010b6c8144 RLMUpdateRealmToSchemaVersion + 196
    5   MyApp                            0x000000010b71476d +[RLMRealm realmWithPath:key:readOnly:inMemory:dynamic:schema:error:] + 4813
    6   MyApp                            0x000000010b713158 +[RLMRealm realmWithPath:readOnly:error:] + 152
    7   MyApp                            0x000000010b712faf +[RLMRealm defaultRealm] + 111
    8   MyApp                            0x000000010b53ee48 _TFC8MyApp11UserManagercfMS0_FT_S0_ + 72
    9   MyApp                            0x000000010b53d0c2 _TFC8MyApp11UserManagerCfMS0_FT_S0_ + 50
    10  MyApp                            0x000000010b53f0a5 _TFFC8MyApp11UserManagerg14sharedInstanceS0_U_FT_T_ + 21
    11  MyApp                            0x000000010b489087 _TTRXFo__dT__XFdCb__dT__ + 39
    12  libdispatch.dylib                   0x000000010fc737f4 _dispatch_client_callout + 8
    13  libdispatch.dylib                   0x000000010fc60343 dispatch_once_f + 565
    14  MyApp                            0x000000010b53cf15 _TFC8MyApp11UserManagerg14sharedInstanceS0_ + 229
    15  MyApp                            0x000000010b53d179 _TToFC8MyApp11UserManagerg14sharedInstanceS0_ + 25
    16  MyApp                            0x000000010b5d2e89 -[AppDelegate application:didFinishLaunchingWithOptions:] + 1097
    17  UIKit                               0x000000010d78e475 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 234
    18  UIKit                               0x000000010d78efbc -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2463
    19  UIKit                               0x000000010d791d2c -[UIApplication _runWithMainScene:transitionContext:completion:] + 1350
    20  UIKit                               0x000000010d790bf2 -[UIApplication workspaceDidEndTransaction:] + 179
    21  FrontBoardServices                  0x0000000112a202a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
    22  CoreFoundation                      0x000000010ece853c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    23  CoreFoundation                      0x000000010ecde285 __CFRunLoopDoBlocks + 341
    24  CoreFoundation                      0x000000010ecde045 __CFRunLoopRun + 2389
    25  CoreFoundation                      0x000000010ecdd486 CFRunLoopRunSpecific + 470
    26  UIKit                               0x000000010d790669 -[UIApplication _run] + 413
    27  UIKit                               0x000000010d793420 UIApplicationMain + 1282
    28  MyApp                            0x000000010b5d7183 main + 115
    29  libdyld.dylib                       0x000000010fca7145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Realm 从不自动生成新线程来完成工作,执行迁移也不例外。

此外,调用 setSchemaVersion:forRealmAtPath:withMigrationBlock: 定义 迁移,但在您第一次访问领域之前不会实际执行它。在您的代码中,我假设 UserManager 单例上的 isUserLoggedIn 方法访问领域,然后触发迁移。

我必须查看创建 UserManager 单例和调用 isLoggedIn 所涉及的代码,以便让您更深入地了解您的应用崩溃的原因。

你确定 Realm 没有抛出异常吗?如果未被捕获,那将使您的应用程序崩溃。如果是这种情况,请在此处分享异常消息。

您可以从我们的文档中了解更多关于迁移在 Realm 中如何工作的信息:http://realm.io/docs/cocoa#migrations