1つの永続ストアを持つ2つの永続ストアコーディネータを作成したいとします。 永続ストアで2つの永続ストアコーディネータを作成する方法
しかし、私は店を追加することができませんが、他のコーディネーターに
- (void) initializeCoreData {
if ([self managedObjectContext]) return;
NSURL *modelUrl = [[NSBundle mainBundle] URLForResource:@"MyModel" withExtension:@"momd"];
NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelUrl];
NSAssert(mom, @"%@:%@ No model to generate a store from", [self class], NSStringFromSelector(_cmd));
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
NSPersistentStoreCoordinator *batchPersistentCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
NSAssert(coordinator, @"Failed to initialize coordinator");
[self setManagedObjectContext:[[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]];
[self setBatchUpdateManagedObjectContext:[[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]];
[self setPrivateContext:[[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]];
[[self batchUpdateManagedObjectContext] setPersistentStoreCoordinator:batchPersistentCoordinator];
[[self privateContext] setPersistentStoreCoordinator:coordinator];
[[self managedObjectContext] setParentContext:[self privateContext]];
dispatch_queue_t queue = NULL;
queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSPersistentStoreCoordinator *psc = [[self privateContext] persistentStoreCoordinator];
NSMutableDictionary *options = [NSMutableDictionary dictionary];
options[NSMigratePersistentStoresAutomaticallyOption] = @(YES);
options[NSInferMappingModelAutomaticallyOption] = @(YES);
options[NSSQLitePragmasOption] = @{@"journal_mode":@"DELETE"};
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *directoryArray = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSURL *storeUrl = nil;
storeUrl = [directoryArray lastObject];
storeUrl = [storeUrl URLByAppendingPathComponent:@"MyModel.sqlite"];
NSError *error = nil;
NSPersistentStore *store = nil;
store = [psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error];
NSAssert(store, @"Error initializing PSC: %@\n%@", [error localizedDescription], [error userInfo]);
psc = [[self batchUpdateManagedObjectContext] persistentStoreCoordinator]
store = [psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error];
NSAssert(store, @"Error initializing PSC: %@\n%@", [error localizedDescription], [error userInfo]);
if (![self initCallBack]) return;
dispatch_sync(dispatch_get_main_queue(), ^{
[self initCallBack]();
});
});
}
なぜあなたはできませんか?試してみるとどうなりますか? –