使用iCloud同步现有Core Data存储

时间:2011-12-31 04:39:26

标签: objective-c core-data icloud

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

在围绕Apple开发论坛和其他地方的一些事情之后,我采取了这种方法,这种方法并不一致。我实际上已经看到它工作,但只有在Device 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;
}

3 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分:2)

至少现在我放弃了iCloud集成。考虑到不稳定的核心数据性能,我觉得我不能通过iCloud为我的用户提供可靠的同步解决方案。我对iOS 5.1抱有希望。

答案 2 :(得分:1)

你有没有成功?

如果没有,请尝试询问 - Weird Errors using CoreData with iCloud

如果是,请给我们解决方案:)