2015-03-19 59 views
1

我在Appdelegate進行遷移,但我也有邏輯來顯示基於來自Realm的UserObject的哪個導航控制器。如何等待Realm.io遷移完成?

[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 

回答

3

境界永遠不會自動派生新的線程來完成工作,並進行遷移沒有例外。

此外,調用setSchemaVersion:forRealmAtPath:withMigrationBlock:定義爲的遷移,但直到您第一次訪問領域時纔會真正執行它。在你的代碼中,我假設UserManager singleton上的isUserLoggedIn方法訪問領域,然後觸發遷移。

我不得不看看創建UserManager單例時所涉及的代碼,並調用isLoggedIn來讓您更深入地瞭解您的應用程序崩潰的原因。

你確定Realm沒有在這裏拋出異常嗎?如果未被捕獲,將會導致您的應用程序崩潰。如果是這種情況,請在這裏分享例外信息。

您可以瞭解更多關於遷移的境界是如何工作的,從我們的文檔:http://realm.io/docs/cocoa#migrations

+0

現在明白了。更新了問題,但我也通過在調用AppDelegate中的UserManager之前刪除了'RLMRealm * realm = [RLMRealm defaultRealm]''代碼來解決該問題。 所以這裏的關鍵是Realm會自動遷移並在第一次調用時採取相應的行動,對吧? 但爲什麼刪除'[RLMRealm defaultRealm]'解決了這個問題? 我曾在的appDelegate以下境界遷移和的UserManager之間代碼 '的NSLog(@ 「ResourcePath%@」,[[一個NSBundle mainBundle] resourcePath]);' '的NSLog(@ 「RealmPath%@」,[RLMRealm defaultRealmPath] );' – Devyn 2015-03-20 09:53:17

+0

我真的建議不要在這裏使用單例模式,因爲這意味着領域屬性不會是線程安全的。如果您在進行此更改後仍然遇到問題,請發送給我們一個我們可以用來重現問題的Xcode項目。 – jpsim 2015-03-20 22:16:54