iOS:将现有的核心数据数据库迁移到 iCloud

标签 ios core-data migration icloud

我在现有应用程序中使用核心数据。现在我想集成 iCloud,以便用户可以在他们的 iOS 设备之间同步他们的内容。为此,我为我的 NSPersistentStoreCoordinator 编写了以下代码(当然占位符已在我的代码中填写):

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

if (persistentStoreCoordinator_ != nil) {
    return persistentStoreCoordinator_;
}

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"<DB_Name>.sqlite"];

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

NSPersistentStoreCoordinator* psc = persistentStoreCoordinator_;

if (IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(@"5.0")) {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSFileManager *fileManager = [NSFileManager defaultManager];

        // Migrate datamodel
        NSDictionary *options = nil;

        // this needs to match the entitlements and provisioning profile
        NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:@"<App_Identifier>"];
        NSString* coreDataCloudContent = [[cloudURL path] stringByAppendingPathComponent:@"data"];
        if ([coreDataCloudContent length] != 0) {
            // iCloud is available
            cloudURL = [NSURL fileURLWithPath:coreDataCloudContent];

            options = [NSDictionary dictionaryWithObjectsAndKeys:
                       [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                       [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                       @"<App_Name>.store", NSPersistentStoreUbiquitousContentNameKey,
                       cloudURL, NSPersistentStoreUbiquitousContentURLKey,
                       nil];
        } else {
            // iCloud is not available
            options = [NSDictionary dictionaryWithObjectsAndKeys:
                       [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                       [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
                       nil];
        }

        NSError *error = nil;
        [psc lock];
        if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
        {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        }
        [psc unlock];

        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"asynchronously added persistent store!");
            [[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:storeURL options:options error:&error])
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    }
}

return persistentStoreCoordinator_;
}

使用该代码,所有新添加的数据记录都会在所有 iOS 设备之间自动同步,因此它可以正常工作!

但我想要的是所有现有数据记录也在所有设备之间同步;那还行不通。现有记录在应用程序中仍然可用并可以使用,但它们未同步。

我需要做什么才能让所有现有数据记录也与 iCloud 同步?我已经尝试了一些方法

migratePersistentStore:toURL:options:withType:error:

但没有任何成功。

我非常感谢任何帮助!

最佳答案

查找 WWDC 2012 session 将 CoreData 与 iCloud 结合使用。他们在最后一节的播种主题下讨论了这个问题。他们还有一些示例代码,您可以从具有示例的站点获得这些代码。简而言之,您将不得不打开 2 个持久存储,设置获取大小,然后从源中抓取批处理,循环遍历它们并将它们添加到新存储中,并以批处理大小为间隔,保存并重置。很简单。

关于iOS:将现有的核心数据数据库迁移到 iCloud,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10329256/

相关文章:

iphone - 使用关系从一对多变为一对一的实体迁移核心数据存储时出现 NSInferredMapping 模型错误

ios - 未调用 Realm 迁移

ios - 使用cordova flle transfer将图像/视频保存到IOS中的图库

objective-c - fileExistsAtPath 为存在的目录返回 NO

ios - 缺少 "aps-environment"权利。注册失败

swift - 在 Swift 中保存核心数据并发时应用程序崩溃

ios - 如何使用 Swift 将对象添加到异步完成 block 中特定索引处的数组

ios - TWICE相关实体的CoreData计数记录

node.js - 使用 sequelize 和 typescript 自动迁移

mysql - 如何将 mysql 列类型从 float 更改为 double 并按原样保存值?