Ios 如何在一个持久存储协调器中创建两个持久存储
我需要创建两个持久性存储,每个都有自己的实体,还有一个持久性存储协调器。最困难的部分是,我希望一个持久存储链接到iCloud,但另一个只能是本地存储。我已经读过关于为托管对象模型进行不同配置的内容,但是如何从本地存储而不是支持iCloud的存储中获取实体呢?这是我到目前为止的代码,我的方向正确吗Ios 如何在一个持久存储协调器中创建两个持久存储,ios,objective-c,core-data,nsmanagedobjectmodel,Ios,Objective C,Core Data,Nsmanagedobjectmodel,我需要创建两个持久性存储,每个都有自己的实体,还有一个持久性存储协调器。最困难的部分是,我希望一个持久存储链接到iCloud,但另一个只能是本地存储。我已经读过关于为托管对象模型进行不同配置的内容,但是如何从本地存储而不是支持iCloud的存储中获取实体呢?这是我到目前为止的代码,我的方向正确吗 NSMutableDictionary *options = [NSMutableDictionary dictionary]; [options setValue:[NSNumber
NSMutableDictionary *options = [NSMutableDictionary dictionary];
[options setValue:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
[options setValue:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];
NSURL *cloudURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
if (cloudURL)
{
NSLog(@"iCloud enabled: %@", cloudURL);
cloudURL = [cloudURL URLByAppendingPathComponent:@"FSListen"];
[options setValue:kICloudContentNameKey forKey:NSPersistentStoreUbiquitousContentNameKey];
[options setValue:cloudURL forKey:NSPersistentStoreUbiquitousContentURLKey];
}
else
{
NSLog(@"iCloud is not enabled");
}
// create the persistent store that will be connected to iCloud for favorites
NSURL *iClouldSoreURL= [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
iClouldSoreURL = [iClouldSoreURL URLByAppendingPathComponent:@"FSListen-iCloud.sqlite"];
NSError *error = nil;
NSPersistentStoreCoordinator *coordinator = [self.managedObjectContext persistentStoreCoordinator];
NSPersistentStore *store = [coordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:@"Default"
URL:iClouldSoreURL
options:options
error:&error];
if (!store)
{
NSLog(@"Error adding persistent store to coordinator %@\n%@", [error localizedDescription], [error userInfo]);
//Present a user facing error
}
// create the persistent store that will not be connected to iCloud for downloads
NSError *downloadsStoreError = nil;
NSURL *downloadsStoreURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
downloadsStoreURL = [downloadsStoreURL URLByAppendingPathComponent:@"FSListen-Downloads.sqlite"];
NSPersistentStore *downloadsStore = [coordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:@"Downloads"
URL:downloadsStoreURL
options:nil
error:&downloadsStoreError];
if (!downloadsStore)
{
NSLog(@"ERROR CREATING DOWNLOADS STORE %@", downloadsStoreError.localizedDescription);
}
在我的托管对象模型中,我有一个配置,其中包含一个名为“下载”的实体,我只希望将其保存在本地,但它也是默认配置,我希望链接到iCloud。如何确保以正确的配置保存实体