iOS:将现有核心数据数据库迁移到 iCloud [英] iOS: Migrating existing Core Data-database into iCloud

查看:23
本文介绍了iOS:将现有核心数据数据库迁移到 iCloud的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Core Data in an existing application. Now I want to integrate iCloud so that the user can synchronize their contents between their iOS-devices. To do that I've written the following code for my NSPersistentStoreCoordinator (of course the placeholders are filled out in my code):

- (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 设备之间自动同步,从而完全按照应有的方式工作!

With that code all new added data records are synchronized automatically between all iOS-devices, so that works exactly the way it should!

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

But what I want is that also all the existing data records are synced between all devices; that doesn't work yet. The existing records are still available within the app and can be used, but they are not synchronized.

我需要做什么才能使所有现有数据记录也与 iCloud 同步?我已经用这个方法做了一些实验

What do I need to do to get all the existing data records synced with iCloud too? I've experimented a bit with the method

migratePersistentStore:toURL:options:withType:error:

但没有任何成功.

非常感谢您的帮助!

推荐答案

查看 WWDC 2012 session Using CoreData with iCloud.他们在最后一节的播种主题下讨论了这个问题.他们也有一些示例代码,您可以从有示例的站点获得.简而言之,您将必须打开 2 个持久存储,设置提取大小,然后从源中获取批次,循环它们并将它们添加到新存储中,并以批次大小为间隔,保存并重置.很简单.

Look up the WWDC 2012 session Using CoreData with iCloud. They discuss this during the final section, under the topic of seeding. They also have some sample code that you can get from the site that has an example. In short, you will have to open 2 persistent stores, set a fetch size, then grab batches from the source, loop through them and add them to the new store, and at batch size intervals, save and reset. Pretty simple.

这篇关于iOS:将现有核心数据数据库迁移到 iCloud的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆