Ios 从iCloud迁移到本地,然后再从本地迁移回iCloud时,必须使用新的NSPersistentStoreUbiquitousContentNameKey
我有一个场景,我当前在应用商店中的应用没有启用iCloud,但随着我正在进行的更新,iCloud将出现在那里。用户可以在应用升级后立即选择是否在开始使用iCloud时使用iCloud,还可以决定在应用中打开/关闭iCloud 现在,如果用户在开始时选择“是”使用iCloud,数据将从应用商店版本(非iCloud)迁移到iCloud,如果用户在应用程序中关闭iCloud,我可以将数据从iCloud商店迁移回本地商店 然而,逻辑告诉我,如果我想让用户在应用程序中再次打开iCloud,它应该将本地应用迁移到iCloud应用商店 以下是首次在Ios 从iCloud迁移到本地,然后再从本地迁移回iCloud时,必须使用新的NSPersistentStoreUbiquitousContentNameKey,ios,core-data,icloud,appdelegate,Ios,Core Data,Icloud,Appdelegate,我有一个场景,我当前在应用商店中的应用没有启用iCloud,但随着我正在进行的更新,iCloud将出现在那里。用户可以在应用升级后立即选择是否在开始使用iCloud时使用iCloud,还可以决定在应用中打开/关闭iCloud 现在,如果用户在开始时选择“是”使用iCloud,数据将从应用商店版本(非iCloud)迁移到iCloud,如果用户在应用程序中关闭iCloud,我可以将数据从iCloud商店迁移回本地商店 然而,逻辑告诉我,如果我想让用户在应用程序中再次打开iCloud,它应该将本地应用
应用程序委托中运行应用程序时从非iCloud到iCloud的初始迁移代码:
- (void)upgradeAndMigrateCoreDataToiCloud {
NSLog(@"Just upgraded your app. On the next run, this does not happen again.");
NSURL *storeURL = [self.persistentStoreCoordinator.persistentStores.lastObject URL];
NSLog(@"Current Store URL (before migration): %@", [storeURL description]);
NSPersistentStore *currentStore = self.persistentStoreCoordinator.persistentStores.lastObject;
URLByAppendingPathComponent:@"NewStore.sqlite"];
NSURL *cloudURL = [self grabCloudPath:@"CloudLogs"];
NSString *cloudStoreTitle = @"EnvyCloud";
NSDictionary *options = @{NSPersistentStoreUbiquitousContentURLKey: cloudURL,
NSPersistentStoreUbiquitousContentNameKey: cloudStoreTitle, NSMigratePersistentStoresAutomaticallyOption : @YES,
NSInferMappingModelAutomaticallyOption : @YES};
[self.persistentStoreCoordinator migratePersistentStore:currentStore toURL:storeURL options:options withType:NSSQLiteStoreType error:nil];
storeURL = [self.persistentStoreCoordinator.persistentStores.lastObject URL];
NSLog(@"Current Store URL (after migration): %@", [storeURL description]);
}
那很好用
以下是用户希望从设置中打开iCloud同步的代码(如果在开始时已关闭,或者用户已关闭,但现在希望重新打开):
这里唯一的区别是我正在调用iCloud通知,我有一个if语句来检查设备上是否启用了iCloud
检查storeURL路径的最后一个NSLog
就是问题所在
在升级方法中,迁移后的storeURL
为:
Current Store URL (after migration): file:///var/mobile/Applications/B07684DE-F74A-4503-A7B3-CD6D82CD087D/Documents/CoreDataUbiquitySupport/mobile~F6322EC0-DA27-4EEB-A6A4-0D3EA875241B/EnvyCloud/3B2005FA-FEEE-4F90-A049-E068B35F09AA/store/Envylope.sqlite
显示已正确迁移
但是,在第二种方法中,从本地迁移到iCloud之前的当前存储url是:
Current Store URL (before migration): file:///var/mobile/Applications/B07684DE-F74A-4503-A7B3-CD6D82CD087D/Documents/Envylope.sqlite
问题
问题是,迁移后,NSLog仍然是:
Current Store URL (before migration): file:///var/mobile/Applications/B07684DE-F74A-4503-A7B3-CD6D82CD087D/Documents/Envylope.sqlite
在该方法中(icloudhasbenenabledfromsettings
),如果我将NSPersistentStoreUbiquitousContentNameKey
更改为稍有不同的名称(以前是EnvyCloud
,并将其更改为例如EnvyCloudNew
),然后它开始工作,NSLog
向我显示CoreDataUbiquityContainer
我肯定是做错了什么,或者这就是它的工作原理,它不能使用“相同的”nsPersistentStoreUbiquityousContentNameKey?
另外,使用不同的NSPersistentStoreUbiquitousContentNameKey是否存在任何问题
更新
我在这方面做了很多工作,并对这个问题做了一些更新,我真的希望有人能帮我
如果我不更改NSPersistentStoreUbiquitousContentNameKey的名称并保持不变,并且如果我通过迁移传递错误参数,控制台中的输出错误为:
Error = Error Domain=NSCocoaErrorDomain Code=134080 "The operation couldn’t be completed. (Cocoa error 134080.)" UserInfo=0x15e9ae90 {NSUnderlyingException=Can't add the same store twice}
这说明它不能两次添加同一个存储
我可以先迁移persistentStore,然后设置\u persistentStoreCoordinator=nil
,然后再添加一个新的persistentStore来解决这个问题-这是可行的,但对我来说似乎不合适。我需要这么做是没有道理的。如果我选择从本地商店返回到iCloud,为什么我不能从iCloud中已经存储的内容重建
我希望这次更新能帮助别人为我指明正确的方向
如有任何想法,将不胜感激
Error = Error Domain=NSCocoaErrorDomain Code=134080 "The operation couldn’t be completed. (Cocoa error 134080.)" UserInfo=0x15e9ae90 {NSUnderlyingException=Can't add the same store twice}