2013-06-03 51 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

相關問題