2011-12-31 78 views
26

我試圖讓iCloud與我的應用一起工作,如果用戶請求,它需要將現有的本地存儲遷移到無處不在的存儲。將現有的Core Data存儲與iCloud同步

在蘋果dev開發論壇和其他地方發生了一些討論後,我採取了這種方法,這種方法並不一致。我已經看到它的工作,但只有在設備B(這是從iCloud填充)幾次崩潰後。

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

    NSURL *legacyStoreUrl = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:[self activeStoreFilenameUpgraded:NO]]]; 
    NSURL *upgradedStoreUrl = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:[self activeStoreFilenameUpgraded:YES]]]; 

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

    if ((IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0")) && (self.iCloudEnabled)) { 
    NSPersistentStoreCoordinator* psc = persistentStoreCoordinator; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    NSDictionary *cloudOptions = nil; 
    NSDictionary *localOptions = [NSDictionary dictionaryWithObjectsAndKeys: 
            [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
            [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, 
            nil]; 


    NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:@"<CONTAINER ID>"]; 
    NSString *coreDataCloudContent = [[cloudURL path] stringByAppendingPathComponent:[NSString stringWithFormat:@"logs%d",[self activeStoreIndex]]]; 
    if ([coreDataCloudContent length] != 0) { 
     // iCloud is available 
     cloudURL = [NSURL fileURLWithPath:coreDataCloudContent]; 

     cloudOptions = [NSDictionary dictionaryWithObjectsAndKeys: 
         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, 
         @"MyAppStore", NSPersistentStoreUbiquitousContentNameKey, 
         cloudURL, NSPersistentStoreUbiquitousContentURLKey, 
         nil]; 
    } else { 
     // iCloud is not available 
    } 

    NSError *error = nil; 
    [psc lock]; 
    if(migrateStore) { 
     migrateStore = NO; 

     NSPersistentStore *srcPS = [psc addPersistentStoreWithType:NSSQLiteStoreType 
      configuration:nil 
      URL:legacyStoreUrl 
      options:localOptions 
      error:&error]; 
     if (![psc migratePersistentStore:srcPS 
      toURL:upgradedStoreUrl 
      options:cloudOptions 
      withType:NSSQLiteStoreType 
      error:&error]) { 
      NSLog(@"Error migrating data: %@, %@/%@/%@", error, [error userInfo], legacyStoreUrl, upgradedStoreUrl); 
      abort(); 
     } 
    } 
    else { 
     if (![psc addPersistentStoreWithType:NSSQLiteStoreType 
      configuration:nil 
      URL:upgradedStoreUrl 
      options:(cloudOptions ? cloudOptions : localOptions) 
      error:&error]) { 
       NSLog(@"Unresolved iCloud error %@, %@", error, [error userInfo]); 
       abort(); 
     } 
    } 
    [psc unlock]; 

    [[NSNotificationCenter defaultCenter] postNotificationName:@"RefetchAllDatabaseData" object:self userInfo:nil]; 
    } else { 
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: 
          [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
          [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, 
          nil]; 

    NSError *error = nil; 
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:legacyStoreUrl options:options error:&error]) { 
     // error 
     abort(); 
    }  
    } 

    return persistentStoreCoordinator; 
} 
+0

只需注意應用程序幾乎總是隻有一個商店;用於處理2個商店(activeStoreIndex等)的情況的代碼是邊緣情況。 – ed94133 2011-12-31 10:46:46

+1

migrateStore在哪裏定義和分配? – Jim 2012-01-09 06:38:14

回答

6

iCloud Core數據同步非常糟糕。更可靠的第三方解決方案是TICoreDataSync; this fork支持通過iCloud進行同步,而無需依賴iCloud的Core Data同步實施。它只是使用iCloud來同步文件並處理獨立同步的核心數據對象。

我放棄了iCloud核心數據同步,而是一直用這個庫來構建我的應用程序;到目前爲止,它一直在努力工作,我沒有看到Apple實施過程中遇到的問題。

+0

TICDS沒有被積極開發。我根據相同的原則開發了一個新的框架[Ensembles](http://ensembles.io),並且說明了與TICDS學到的經驗教訓。 – 2015-01-01 09:21:54

2

我放棄了iCloud的整合,至少現在。考慮到不穩定的Core Data性能,我不認爲我可以通過iCloud爲我的用戶提供可靠的同步解決方案。我對iOS 5.1抱有希望。

+4

我工作的公司也希望將iCloud集成到現有的具有核心數據的應用程序中,但我告訴他們它無法正常工作。這就像地獄!在2月份發佈後,我們得到了很多關於崩潰和丟失數據的負面反饋。該公司在八月份破產:D只想講故事... – 2012-10-05 14:28:04

相關問題