それぞれ独自のエンティティーを持つ 2 つの永続ストアを作成し、1 つの永続ストア コーディネーターを作成する必要があります。難しいのは、1 つの永続的なストアを iCloud にリンクし、もう 1 つのストアをローカル ストアのみにすることです。管理対象オブジェクト モデルのさまざまな構成について読んだことがありますが、iCloud 対応ストアではなく、ローカル ストアからエンティティを取得するにはどうすればよいですか? これまでのコードは次のとおりです。正しい方向に向かっていますか?:
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);
}
私のマネージド オブジェクト モデルには、ローカルにのみ保存したい「ダウンロード」というエンティティを含む 1 つの構成がありますが、これは iCloud にリンクしたい既定の構成でもあります。エンティティが正しい構成で保存されていることを確認するにはどうすればよいですか?