gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:53:55 30 4
gpt4 key购买 nike

我在 Appdelegate 中进行迁移但我也有逻辑显示哪个导航 Controller 基于来自 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()
}
}

异常(exception)
*** 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 永远不会自动生成新线程来完成工作,执行迁移也不异常(exception)。

另外,调用setSchemaVersion:forRealmAtPath:withMigrationBlock:将定义一个迁移,但在您第一次访问该 Realm 之前不会真正执行它。在您的代码中,我假设 isUserLoggedIn UserManager 上的方法单例访问 Realm ,然后触发迁移。

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

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

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

关于ios - 如何等到 Realm.io 迁移完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29140733/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com