这是你需要做的
创建本地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
}];
}];
</code>
</醇>
这是代码,在线注释。
请注意,执行此迁移后,您需要使用新URL配置与MOC一起使用的持久性存储,并始终使用NSPersistentStoreUbiquitousContentNameKey键包含上面的iCloudOptions。
这是基于
<a href =“https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/UsingCoreDataWithiCloudPG/UsingSQLiteStoragewithiCloud/UsingSQLiteStoragewithiCloud.html#//apple_ref/doc/uid/TP40013491-CH3-SW3"rel =” noreferrer“>
Apple的文档
。
完成后,您应该在模拟器文件夹(〜/ Library / Application Support / iPhone Simulator / …)中的Documents文件夹中看到一个名为CoreDataUbiquitySupport的新文件夹。嵌套在你的iCloud同步sqlite商店深处。
田田!
编辑:哦,并确保您已创建一个iCloud权利,并将其包含在您的捆绑包中。您应该能够在Xcode中完成所有操作,但您也可以在开发门户上更新它。