2013-06-03 53 views
0

根据我是否在模拟器中,我现在有权立即触发iCloud加载。当我尝试在真实设备上运行时,出现黑屏,'addPersistentStore'行似乎挂起。 “我的项目名称”是权利文件的名称和应用程序的名称。尝试在手机上启动iCloud应用程序时出现黑屏

发生了什么事?

#if (TARGET_IPHONE_SIMULATOR) 
     if (![psc addPersistentStoreWithType:NSSQLiteStoreType 
           configuration:nil 
             URL:dbUrl 
            options:nil 
             error:&error]) { 
      [NSException raise:@"Open failed" format:@"Reason: %@", [error localizedDescription]]; 
     } 
#else 
     NSFileManager *fm = [NSFileManager defaultManager]; 
     NSURL *ubContainer = [fm URLForUbiquityContainerIdentifier:nil]; 
     NSMutableDictionary *options = [NSMutableDictionary dictionary]; 
     [options setObject:@"My Project Name" forKey:NSPersistentStoreUbiquitousContentNameKey]; 
     [options setObject:ubContainer forKey:NSPersistentStoreUbiquitousContentURLKey]; 

     if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:dbUrl options:options error:&error]) { 
      [NSException raise:@"Open failed" format:@"%@", [error localizedDescription]]; 
     } 
#endif 

回答

2

Apple建议您在使用iCloud时,应该在单独的线程上执行所有这些步骤。 URLForUbiquityContainerIdentifieraddPersistentStoreWithType:configuration:options:error:都将连接到网络,并且可能会长时间阻塞。第二次调用 - 添加持久性存储 - 可能会阻塞很长时间。在iOS上,只能按需下载iCloud数据,并且在添加持久性存储时会发生此需求。由于NSPersistentStoreCoordinator正在忙于与网络通话(或正在尝试这样做),因此您会看到空白屏幕。 Apple的示例代码将其放在单独的队列中,您也应该这样做。

0

您的代码没有说明这一点,但您不能在主线程上调用-URLForUbiquityContainerIdentifier。从Apple documentation注:

Important: Do not call this method from your app’s main thread. Because this method might take a nontrivial amount of time to set up iCloud and return the requested URL, you should always call it from a secondary thread. To determine if iCloud is available, especially at launch time, call the ubiquityIdentityToken method instead.

这很可能是,它需要很长的时间,它看起来好像你的应用程序无法加载,而实际上它只是在等待该方法返回。

+0

否。该行返回正常。这是挂起的addPersistentStore。 –

+1

我仍然会尝试在不同的线程上运行它。在这个SO问题有人有相同的问题:http://stackoverflow.com/questions/12923238/ios-application-freezing-on-first-launch-when-icloud-is-enabled –

+0

这是好的建议,无论哪种方式 – DGund

相关问题