事情真的很棒.但是,当用户从系统首选项启用或禁用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的新创建的Core Data.,这段代码非常适合应用程序(非常不稳定).我正在寻找更通用的解决方案).
谢谢
解决方法 我有完全相同的问题,发现这个 https://gist.github.com/mluisbrown/7015953.现在对我来说很完美. 总结以上是内存溢出为你收集整理的ios – 核心数据:处理iCloud关闭/打开时的情况全部内容,希望文章能够帮你解决ios – 核心数据:处理iCloud关闭/打开时的情况所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)