我想将iCloud添加到我的核心数据应用中。我想从iCloud中唯一想要的是通过删除和重新安装App来持久保存数据。我目前不需要为该应用程序提供多个设备支持。我已经使用Core Data管理了iCloud工作。但是,有一种情况我不知道如何处理。现在,如果iCloud启用了应用程序,iCloud中保存的数据将通过删除和重新安装应用程序而持续存在,这正是我想要的。但是当用户关闭应用程序的iCloud时,iCloud中保存的数据不再显示我认为是正常的,当用户创建新数据时,新数据将保存在应用程序的本地存储中。所以看起来我有两个独立的存储空间,一个是iCloud,另一个是本地存储。所以我的问题是,当iCloud关闭并创建数据值并保存到应用程序的本地存储中时,如何确保将这些数据值添加到iCloud中保存的数据值,当iCloud转向时回来?
以下是我的App代表的相关部分:
- (NSManagedObjectContext *)managedObjectContext {
if (_managedObjectContext != nil)
{
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator)
{
return nil;
}
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
return _managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel
{
if (_managedObjectModel != nil)
{
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
NSURL *documentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSURL *storeURL = [documentsDirectory URLByAppendingPathComponent:@"Model.sqlite"];
NSError *error = nil;
NSString *failureReason = @"There was an error creating or loading the application's saved data.";
NSDictionary *storeOptions =
@{NSPersistentStoreUbiquitousContentNameKey: @"MyAppCloudStore"};
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:storeOptions error:&error]) {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
dict[NSLocalizedFailureReasonErrorKey] = failureReason;
dict[NSUnderlyingErrorKey] = error;
error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}