我在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
现在明白了。更新了问题,但我也通过在调用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
我真的建议不要在这里使用单例模式,因为这意味着领域属性不会是线程安全的。如果您在进行此更改后仍然遇到问题,请发送给我们一个我们可以用来重现问题的Xcode项目。 – jpsim 2015-03-20 22:16:54