2011-12-31 35 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

相关问题