使用iCloud同步现有Core Data存储

ed9*_*133 26 core-data objective-c icloud

我试图让iCloud与我的应用程序一起工作,如果用户请求,它需要将现有的本地存储迁移到无处不在的商店.

经过围绕Apple开发论坛和其他地方的一些关注之后,我采用了这种方法,这种方法并不一致.我实际上看到它工作,但只有在设备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;
}
Run Code Online (Sandbox Code Playgroud)

Xta*_*etl 6

iCloud Core Data同步非常糟糕.更可靠的第三方解决方案是TICoreDataSync; 这个分支支持同步iCloud,而不依赖于iCloud的核心数据同步实现.它只是使用iCloud同步文件并自行处理Core Data对象同步.

我放弃了iCloud Core Data同步,并且一直在用这个库构建我的应用程序; 到目前为止它一直很好用,没有我在Apple的实现中遇到的问题.