如何在已使用本地存储 Core Data 的应用程序中启用 iCloud Core Data?
我尝试过使用NSPersistentStoreUbiquitousContentNameKey
在我的持久存储选项中。不幸的是,此选项启用 iCloud,但不会将任何本地数据传输到 iCloud。我似乎无法得到migratePersistentStore:toURL:options:withType:error:
要么工作。我提供了持久存储、其 URL、iCloud 选项等,但它仍然不会将现有的本地数据迁移到 iCloud。这是我使用该方法的方式:
- (void)migratePersistentStoreWithOptions:(NSDictionary *)options {
NSError *error;
self.storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.sqlite", self.SQLiteFileName]];
NSPersistentStore *store = [self.persistentStoreCoordinator migratePersistentStore:self.persistentStoreCoordinator.persistentStores.firstObject toURL:self.storeURL options:options withType:NSSQLiteStoreType error:&error];
if (store) NSLog(@"[CoreData Manager] Store was successfully migrated");
else NSLog(@"[CoreData Manager] Error migrating persistent store: %@", error);
}
本地存储与 iCloud 存储保持分离。如果可能的话,我想将本地核心数据移动到 iCloud,而无需手动传输每个实体。
有任何想法吗?我可以找到很多有关移回本地存储的文章、教程和帖子fromiCloud - 但我想移动from本地存储to iCloud.
这是你需要做的
- 创建本地 NSPersistentStoreCoordinator
- 将现有的持久存储添加到该协调器并存储对此新返回的存储的引用。
- 调用这个方便的 migratePersistStore:... 提供 #2 中的存储、文档目录中具有不同文件名的存储的 URL 以及包括 NSPersistentStoreUbiquitousContentNameKey 键在内的所有重要选项。
这是代码,内嵌注释。
NSURL *documentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
//This is the path to the new store. Note it has a different file name
NSURL *storeURL = [documentsDirectory URLByAppendingPathComponent:@"TestRemote.sqlite"];
//This is the path to the existing store
NSURL *seedStoreURL = [documentsDirectory URLByAppendingPathComponent:@"Test.sqlite"];
//You should create a new store here instead of using the one you presumably already have access to
NSPersistentStoreCoordinator *coord = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
NSError *seedStoreError;
NSDictionary *seedStoreOptions = @{ NSReadOnlyPersistentStoreOption: @YES };
NSPersistentStore *seedStore = [coord addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:seedStoreURL
options:seedStoreOptions
error:&seedStoreError];
NSDictionary *iCloudOptions = @{ NSPersistentStoreUbiquitousContentNameKey: @"MyiCloudStore" };
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//This is using an operation queue because this happens synchronously
[queue addOperationWithBlock:^{
NSError *blockError;
[coord migratePersistentStore:seedStore
toURL:storeURL
options:iCloudOptions
withType:NSSQLiteStoreType
error:&blockError];
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[mainQueue addOperationWithBlock:^{
// This will be called when the migration is done
}];
}];
请注意,执行此迁移后,您需要使用新 URL 配置与 MOC 一起使用的持久存储,并始终包含上面的 iCloudOptions 和 NSPersistentStoreUbiquitousContentNameKey 键。
这是基于苹果的文档 https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/UsingCoreDataWithiCloudPG/UsingSQLiteStoragewithiCloud/UsingSQLiteStoragewithiCloud.html#//apple_ref/doc/uid/TP40013491-CH3-SW3.
完成后,您应该会在模拟器文件夹 (~/Library/Application Support/iPhone Simulator/...) 的 Documents 文件夹中看到一个标有 CoreDataUbiquitySupport 的新文件夹。嵌套在里面的是你的 iCloud 同步 sqlite 存储。
Tada!
编辑:哦,请确保您已创建 iCloud 权利并将其包含在您的捆绑包中。您应该能够在 Xcode 中完成所有这些操作,但您也可以在开发门户上更新它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)