[英]Move local Core Data to iCloud
How can I enable iCloud Core Data in an app which already uses local storage Core Data? 如何在已使用本地存储核心数据的应用程序中启用iCloud核心数据?
I've tried to use NSPersistentStoreUbiquitousContentNameKey
in my persistent store options. 我试图在我的持久存储选项中使用
NSPersistentStoreUbiquitousContentNameKey
。 Unfortunately, this option enables iCloud but does not transfer any of the local data to iCloud. 不幸的是,此选项启用iCloud但不会将任何本地数据传输到iCloud。 I can't seem to get
migratePersistentStore:toURL:options:withType:error:
to work either. 我似乎无法获得
migratePersistentStore:toURL:options:withType:error:
工作任何一个。 I provide the persistent store, its URL, iCloud options, etc. and it still will not migrate the existing local data to iCloud. 我提供持久性存储,URL,iCloud选项等,它仍然不会将现有的本地数据迁移到iCloud。 Here's how I'm using the method:
这是我使用该方法的方式:
- (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);
}
The local storage remains separate from the iCloud storage. 本地存储与iCloud存储分开。 If possible, I'd like to move the local Core Data to iCloud without manually transferring each entity.
如果可能的话,我想将本地Core Data移动到iCloud,而无需手动转移每个实体。
Any ideas? 有任何想法吗? I can find lots of articles, tutorials, and posts about moving back to local storage from iCloud - but I want to move from local storage to iCloud .
我可以找到很多关于从 iCloud移回本地存储的文章,教程和帖子 - 但我想从本地存储迁移到 iCloud 。
Here's what you'll need to do 这是你需要做的
Here's the code, notes in-line. 这是代码,在线注释。
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
}];
}];
Note that after you do this migration, you'll need to configure the persistent store you use with your MOC with the new URL and always include the iCloudOptions above with the NSPersistentStoreUbiquitousContentNameKey key. 请注意,执行此迁移后,您需要使用新URL配置与MOC一起使用的持久性存储,并始终使用NSPersistentStoreUbiquitousContentNameKey键包含上面的iCloudOptions。
This was based on Apple's documentation . 这是基于Apple的文档 。
After completion, you should see a new folder in your Documents folder in the simulator folder (~/Library/Application Support/iPhone Simulator/...) labeled CoreDataUbiquitySupport. 完成后,您应该在模拟器文件夹(〜/ Library / Application Support / iPhone Simulator / ...)中的Documents文件夹中看到一个名为CoreDataUbiquitySupport的新文件夹。 Nested deep in there is your iCloud synced sqlite store.
嵌套在你的iCloud同步sqlite商店深处。
Tada! 田田!
EDIT: Oh and make sure you have created an iCloud entitlement and included it in your bundle. 编辑:哦,并确保您已创建一个iCloud权利,并将其包含在您的捆绑包中。 You should be able to do that all within Xcode, but you can update it on the development portal too.
您应该能够在Xcode中完成所有操作,但您也可以在开发门户上更新它。
Take a look at this sample app which includes code to migrate a local core data store to iCloud and back again. 看看这个示例应用程序,其中包含将本地核心数据存储迁移到iCloud并再次返回的代码。 Best read the associated docs and build the sample apps in your environment to get them working and once they are working then try and refactor your code to use a similar approach.
最好阅读相关文档并在您的环境中构建示例应用程序以使它们正常工作,一旦它们正在工作,然后尝试重构您的代码以使用类似的方法。
Feel free to send me an email for further help. 随时给我发电子邮件以获得进一步的帮助。 Apologies for not giving you an answer here but it can be quite a complicated issue to deal with.
抱歉没有给你答案,但处理起来可能是一个非常复杂的问题。
http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/ http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.