大佬教程收集整理的这篇文章主要介绍了ios – 将现有核心数据迁移到iCloud,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我现在正在将iCloud同步到我的环境中.参考之前提出的问题:Existing Core Data Database Not Uploading to iCloud When Enabling iCloud On My App,我仍然遇到问题,但是用我的新代码更新问题太麻烦了.我指的是WWDC 2013 CoreData Sessions,以及this.因为我的应用只是iOS 7.我是iCloud开发的新手,也是一名中级程序员.
当我从App Store(非iCloud)版本更新到开发(iCloud)版本时,我将现有数据迁移到iCloud时遇到很大困难.新数据完美同步.
据我所知,我需要在没有iCloud的情况下将我现有的商店迁移到iCloud,但这对我来说并不合适.
这里有一些代码来说明我正在做的事情:
更新
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (_persistentStoreCoordinator != nil) { return _persistentStoreCoordinator; } NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"PlAnner.sqlite"]; NSFileManager *fm = [NSFileManager defaultManager]; NSURL *url = [fm URLForUbiquityContainerIdentifier:nil]; NSError *error = nil; _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; if (url) { NSLog(@"iCloud is enabled"); NSDictionary *options = @{ NSMigratePersistentStoresAutomaticallyOption : @YES,NSInferMappingModelAutomaticallyOption : @YES,}; NSPersistentStore *localStore = [_persistentStoreCoordinator addPersistentStoreWithType:NSsqliteStoreType configuration:nil URL:storeURL options:options error:&error]; [_persistentStoreCoordinator migratePersistentStore:localStore toURL:[self iCloudURL] options:[self iCloudStoreOptions] withType:NSsqliteStoreType error:&error]; } else { NSLog(@"iCloud is not enabled"); NSDictionary *options = @{ NSMigratePersistentStoresAutomaticallyOption : @YES,NSInferMappingModelAutomaticallyOption : @YES }; if (![_persistentStoreCoordinator addPersistentStoreWithType:NSsqliteStoreType configuration:nil URL:[self iCloudURL] options:options error:&error]) { NSLog(@"Unresolved error %@,%@",error,[error userInfo]); abort(); } } dispatch_async(dispatch_get_main_queue(),^{ [[NsnotificationCenter defaultCenter] postNotificationName:@"Reload" object:self userInfo:nil]; }); return _persistentStoreCoordinator; }
我不再@L_652_16@migrateLocalStoreToiCloud方法,但它在这里供参考:
/*-(void)migrateLocalStoreToiCloud { //assuming you only have one store. NSPersistentStore *store = [[_persistentStoreCoordinator persistentStores] firstObject]; NSPersistentStore *newStore = [_persistentStoreCoordinator migratePersistentStore:store toURL:[self iCloudURL] options:[self iCloudStoreOptions] withType:NSsqliteStoreType error:nil]; [self reloadStore:newStore]; }
* /
storeURL,storeOptions,iCloudURL和iCloudStoreOptions代码是:
- (NSURL *)iCloudURL { NSFileManager *fm = [NSFileManager defaultManager]; NSURL *url = [fm URLForUbiquityContainerIdentifier:nil]; if (url) { NSLog(@"iCloud access at %@",url); } else { NSLog(@"No iCloud access"); } return url; } -(NSURL *)storeURL{ return [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"PlAnner.sqlite"]; } -(NSDictionary *)storeOptions{ NSMutableDictionary *options = [[NSMutableDictionary alloc] init]; [options setObject:[NSnumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption]; [options setObject:[NSnumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption]; return options; } -(NSDictionary *)iCloudStoreOptions{ NSMutableDictionary *options = [[NSMutableDictionary alloc] init]; [options setObject:[NSnumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption]; [options setObject:[NSnumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption]; [options setObject:@"PlAnnerStore" forKey:NSPersistentStoreUbiquitousContentNameKey]; return options; }
问题
当我完全按原样运行此代码时,我没有在使用本地存储的控制台中显示:1和0,尽管数据在那里.
如果我更改persistentStoreCoordinator,它首先添加persistentStore以使用[self iCloudStoreOptions]作为选项,则显示使用本地存储1和0,但数据不在设备上.
我正在遵循这里的指导,我尝试了多种代码,包括不在reloadStore方法中删除任何persistentStore,但我完全卡住了.
我已经阅读了这么多帖子,但似乎我找不到将数据从非iCloud版本迁移到iCloud版本的人,并且我找到的示例中没有代码.
我已经查看了this,虽然它是一个很棒的代码集,但我已经下载了代码并遵循了那些不删除任何persistentStore的指南,但它仍然不适用于我.我无法想象我想要实现的目标非常复杂.我只需要获取现有的非iCloud数据并将其迁移到iCloud.
更新
阅读Apple的指南:https://developer.apple.com/LIBRARY/ios/documentation/DataManagement/Conceptual/UsingCoreDataWithiCloudPG/UsingSQLiteStoragewithiCloud/UsingSQLiteStoragewithiCloud.html#//apple_ref/doc/uid/TP40013491-CH3-SW2我可以看到我的migrateLocaltoiCloud方法目前在删除persistentStore等方面是错误的,但我无法弄清楚如何修复它.如果我从persistentStoreCoordinator调用它,它会以某种方式干扰DidChangeNotification吗?
我真的坚持这一点,我希望任何指导正确的方向
通过调试addPersistentStore和migratePersistentStore代码行,我想出了:
NSPersistentStore *localStore = [_persistentStoreCoordinator addPersistentStoreWithType:NSsqliteStoreType configuration:nil URL:storeURL options:options error:&error]; NSLog(@"The localStore is %@",localStorE); NSPersistentStore *migratedStore = [_persistentStoreCoordinator migratePersistentStore:localStore toURL:[self iCloudURL] options:[self iCloudStoreOptions] withType:NSsqliteStoreType error:&error]; NSLog(@"The migrationStore is %@",migratedStorE); The NSLog value of localStore is: The localStore is <NSsqlCore: 0x13451b100> (URL: file:///var/mobile/Applications/DDB71522-C61D-41FC-97C7-ED915D6C46A4/Documents/PlAnner.sqlitE) The NSLog value of migratedStore is: (null)
通过调试,我可以看到基本相同的东西,migrateStore始终为null.
我稍微更改了代码:
NSPersistentStore *migratedStore = [_persistentStoreCoordinator migratePersistentStore:localStore toURL:storeURL options:[self iCloudStoreOptions] withType:NSsqliteStoreType error:&error];
要使URL成为storeURL,突然之间,一切都开始工作了,其中storeURL:
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"PlAnner.sqlite"];
我的产出是:
The localStore is <NSsqlCore: 0x14ee09b50> (URL: file:///var/mobile/Applications/CC0F7DA0-251F-46D6-8E43-39D63062AED2/Documents/PlAnner.sqlitE) [PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](771): CoreData: Ubiquity: mobile~5BD59259-7758-42FB-986F-DDD173F75ED3:EnStor The migrationStore is <NSsqlCore: 0x157e260b0> (URL: file:///var/mobile/Applications/A08D183D-ECD4-4E72-BA64-D21727DE7A3E/Documents/CoreDataUbiquitySupport/mobile~CB6C3699-8BF1-47DB-9786-14BCE1CD570A/EnStor/771D2B0B-870A-428E-A9A8-5DAE1295AF53/store/PlAnner.sqlitE)
使用本地存储从1到0,应用程序的App Store版本中的数据继续通过迁移到新版本.这是我在升级后第一次运行应用程序时的输出.
这是个好消息.
随后我运行应用程序时,数据继续迁移,因此我最终会得到重复的结果.
问题还在于,在添加新设备时,它不会将信息下载到新设备.
我不太明白为什么使用storeURL作为migratePersistentStore的url工作?
我也不希望每个新设备基本上添加一个新的持久性存储(如果已存在)并且我不知道如何解决这个问题并且我也不希望一次又一次地进行迁移.它应该只发生一次.
我到了那里,但还有很多工作@L_801_65@!
以上是大佬教程为你收集整理的ios – 将现有核心数据迁移到iCloud全部内容,希望文章能够帮你解决ios – 将现有核心数据迁移到iCloud所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。