gpt4 book ai didi

ios - 核心数据 : handle the case when iCloud turns off/on

转载 作者:可可西里 更新时间:2023-11-01 06:09:41 27 4
gpt4 key购买 nike

我正在我的 Core Data 应用程序中实现 iCloud 支持(仅限 iOS 7,尚未发布,iCloud 支持将从第一天开始)。我查看了关于 iCloud 变化的 WWDC 2013 207 session ,我真的很高兴看到这些改进(我以前也有一些 iCloud 的经验)。

一切都很好。但是,我不确定如何处理用户从系统偏好设置中启用或禁用 iCloud 的情况——这会导致在另一个目录中创建新的 .sqlite 文件,从而导致用户数据丢失。

以下是我如何实现持久性存储添加:

- (void) addPersistentStoreToCoordinator {

NSMutableDictionary *options = [NSMutableDictionary dictionary];
[options setObject:@YES forKey:NSMigratePersistentStoresAutomaticallyOption];
[options setObject:@YES forKey:NSInferMappingModelAutomaticallyOption];

NSURL *iCloud = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier: nil];

if (iCloud) {
[options setObject:@"ABC123~com~myapp~myapp" forKey:NSPersistentStoreUbiquitousContentNameKey];
}

NSError* error;

// the only difference in this call that makes the store an iCloud enabled store
// is the NSPersistentStoreUbiquitousContentNameKey in options.

[persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:self.storeURL
options:options
error:&error];
}

- (NSURL *)storeURL {

NSURL *documentsDirectory = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:YES
error:NULL];

return [documentsDirectory URLByAppendingPathComponent:@"MyApp.sqlite"];
}

当用户在设置中关闭/打开 iCloud 首选项时,如何从 Core Data 保存数据?

(旁注:我已经设法手动处理 iCloud 从状态 [OFF] > [ON] 的情况——从旧的 Core Data 获取所有数据,然后将这些 NSManagedObject 子类保存到启用 iCloud 的新创建的核心数据中。但是,这段代码非常特定于应用程序(并且非常不稳定)。我正在寻找更通用的解决方案。

谢谢

最佳答案

我有完全相同的问题并找到了这个 https://gist.github.com/mluisbrown/7015953 .现在非常适合我。

关于ios - 核心数据 : handle the case when iCloud turns off/on,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19908803/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com