2013-01-23 65 views
2

我在我的persistentStoreCoordinator中有以下代碼。沒有iCloud部分,它工作正常。使用iCloud時,它停在addPersistentStoreWithType方法上。沒有錯誤,它只是停下來,不會繼續。應用程序在addPersistentStoreWithType與iCloud掛起

有什麼想法?

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
    if (_persistentStoreCoordinator != nil) { 
     return _persistentStoreCoordinator; 
    } 

    NSURL *storeURL = [NSURL fileURLWithPath:STORE_PATH]; 

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 


    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

    NSMutableDictionary *options = [NSMutableDictionary dictionary]; 
    [options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption]; 
    [options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption]; 


     NSFileManager *fileManager = [NSFileManager defaultManager]; 

     NSURL *iCloud = [fileManager URLForUbiquityContainerIdentifier:nil]; 
     NSLog(@"icloud: %@", iCloud); 

     if (iCloud) { 

      NSString *iCloudLogsDirectoryName = @"Logs"; 
      NSURL *iCloudLogsPath = [NSURL fileURLWithPath:[[iCloud path] stringByAppendingPathComponent:iCloudLogsDirectoryName]]; 

      //Create logs directory, in case it doesn't exist 
      if([fileManager fileExistsAtPath:[iCloudLogsPath path]] == NO) { 
       NSLog(@"logs directory doesn't exist"); 
       NSError *fileSystemError; 
       [fileManager createDirectoryAtPath:[[iCloud path] stringByAppendingPathComponent:iCloudLogsDirectoryName] 
         withIntermediateDirectories:YES 
             attributes:nil 
              error:&fileSystemError]; 
       if(fileSystemError != nil) { 
        NSLog(@"Error creating logs directory %@", fileSystemError); 
       } 
      } 

      NSString *iCloudEnabledAppID = @"app id removed from stackoverflow"; 

      [options setObject:iCloudEnabledAppID   forKey:NSPersistentStoreUbiquitousContentNameKey]; 
      [options setObject:iCloudLogsPath    forKey:NSPersistentStoreUbiquitousContentURLKey]; 
      NSLog(@"logs path: %@", iCloudLogsPath); 
     } 

    [_persistentStoreCoordinator lock]; 

    NSError *error; 

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                configuration:nil 
                  URL:storeURL 
                 options:options 
                  error:&error]) { 

     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 
    else { 
     NSLog(@"done adding persistent store"); 
    } 

    [_persistentStoreCoordinator unlock]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"SomethingChanged" object:self userInfo:nil]; 
     [self.delegate contextWasSetupInManager:self]; 
    }); 

    }); 

    return _persistentStoreCoordinator; 

} 

回答

2

這在iCloud中很常見。 Apple建議您將其置於後臺隊列中,因爲該呼叫可能會阻止。它會阻止,因爲這是Core Data實際連接到iCloud服務器的地方,並開始下載任何可用的新數據。通話不會繼續,直到完成。如果有大量數據需要下載,您可能需要等一會兒。

有時需要一段時間沒有明顯的原因。就像那樣。

最終,您所看到的與現在iCloud的情況大致相同。提出一兩個錯誤,希望事情能夠改善。

+0

我能否允許用戶在我的應用程序中繼續使用核心數據,並且此方法在後臺運行? – Andrew

+0

不幸的是沒有。只要需要運行,就可以讓該方法運行,但在完成之前,您沒有數據存儲。這意味着你不能從Core Data獲取任何內容。我已經看到了一些關於如何處理這個問題的想法,但沒有一個真的很成功。 –

+0

但是,那不是那種......毀掉我的用戶體驗,只要希望(分鐘,小時)把用戶保持在同一點,並且不讓他們使用該應用程序,iCloud就可以花費多長時間?當然有一種解決方法,一個替代方案? – Andrew

1

我剛剛在我的一個測試設備(運行iOS 6.1的iPhone 5)上遇到過這個問題。進行工廠重置,然後從手機的當前備份中恢復固定它。

我不確定這是否是解釋,但似乎存在通過設備設置> iCloud>存儲&備份刪除應用程序的iCloud無所不在容器數據的問題。這可能會使設備進入無處不在的容器殘留物無法移除的狀態。在設備上進行全面擦除和恢復似乎可以解決此問題。

我發現一個看起來像是涉及到這個問題的舊帖子(http://stackmonthly.com/2011/11/core-data),並在OpenRadar上的這個條目(http://openradar.appspot.com/10440734)。我把它搞亂了幾個小時,直到我擦拭並恢復完美的設備:addPersistentStoreWithType幾乎立即完成。

展望未來我將嘗試使用WWDC 2012示例應用程序SharedCoreData(會話227)中的nukeAndPave方法清除商店,而不是從Settings中手動刪除商店。希望這可以防止這種情況再次發生。

0

我不完全知道爲什麼這些問題上來,但是我已經在做的手機的設置復位避過這個性質的問題有很多成功的:

設置>常規>重置>重置所有設置