Ios 具有核心数据的iCloud,iCloud关闭时的数据持久性

Ios 具有核心数据的iCloud,iCloud关闭时的数据持久性,ios,core-data,icloud,Ios,Core Data,Icloud,我想将iCloud添加到我的核心数据应用程序中。我唯一想从iCloud中得到的是通过删除和重新安装应用程序来持久化数据。我目前不需要该应用程序的多设备支持。我用核心数据管理了iCloud的工作。然而,有一种情况我不知道如何处理。现在,如果应用程序的iCloud已打开,则iCloud中保存的数据将通过删除和重新安装应用程序而保持不变,这正是我想要的。但当用户关闭应用程序的iCloud时,iCloud中保存的数据不再显示,我认为这是正常的,当用户创建新数据时,新数据将保存在应用程序的本地存储中。看起

我想将iCloud添加到我的核心数据应用程序中。我唯一想从iCloud中得到的是通过删除和重新安装应用程序来持久化数据。我目前不需要该应用程序的多设备支持。我用核心数据管理了iCloud的工作。然而,有一种情况我不知道如何处理。现在,如果应用程序的iCloud已打开,则iCloud中保存的数据将通过删除和重新安装应用程序而保持不变,这正是我想要的。但当用户关闭应用程序的iCloud时,iCloud中保存的数据不再显示,我认为这是正常的,当用户创建新数据时,新数据将保存在应用程序的本地存储中。看起来我有两个独立的存储器,一个是iCloud,一个是local。所以我的问题是,当iCloud关闭并且数据值被创建并保存到应用程序的本地存储中时,当iCloud重新打开时,如何确保这些数据值被添加到iCloud中保存的数据值中

以下是我的应用程序委托的相关部分:

- (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;
}