Core data 设置持久存储和父上下文
当Core data 设置持久存储和父上下文,core-data,nspersistentstore,Core Data,Nspersistentstore,当NSManagedObjectContext同时设置了持久存储和父上下文并调用save时会发生什么情况?它会将数据一个接一个地推送到持久存储和父上下文吗?还是会同时进行?或者核心数据只是抛出一个抱怨异常 API不会直接阻止一个人为给定上下文设置两个“父项”。这将发生: ***由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“上下文已经有一个协调器;无法替换。“ 这是因为当您设置parentContext时,persistentstore
NSManagedObjectContext
同时设置了持久存储和父上下文并调用save时会发生什么情况?它会将数据一个接一个地推送到持久存储和父上下文吗?还是会同时进行?或者核心数据只是抛出一个抱怨异常
API不会直接阻止一个人为给定上下文设置两个“父项”。这将发生:
***由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“上下文已经有一个协调器;无法替换。“
这是因为当您设置parentContext
时,persistentstorecordinator
会自动设置为父上下文的persistentstorecordinator
这将发生:
***由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“上下文已经有一个协调器;无法替换。“
这是因为当您设置
parentContext
时,persistentstorecordinator
会自动设置为父上下文的persistentstorecordinator
如果为父上下文分配协调器,则不需要为托管上下文设置协调器
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[self setManagedObjectContext:moc];
[self setPrivateContext:[[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]];
/// when you set parentContext, the persistentStoreCoordinator is automatically set to the persistentStoreCoordinator of the parent contex
[self.privateContext setPersistentStoreCoordinator:coordinator];
[self.managedObjectContext setParentContext:self.privateContext];
以下是在框架示例中使用的完整代码-
NSURL *modelURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TVModelSDK" withExtension:@"momd"];
NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
NSAssert(mom, @"Failed to initialize mom from URL: %@", modelURL);
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
/// DONOT set coordinator for managed Context !
// [moc setPersistentStoreCoordinator:coordinator];
[self setManagedObjectContext:moc];
[self setPrivateContext:[[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]];
/// when you set parentContext, the persistentStoreCoordinator is automatically set to the persistentStoreCoordinator of the parent contex
[self.privateContext setPersistentStoreCoordinator:coordinator];
[self.managedObjectContext setParentContext:self.privateContext];
如果为父上下文分配协调器,则不需要为托管上下文设置协调器
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[self setManagedObjectContext:moc];
[self setPrivateContext:[[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]];
/// when you set parentContext, the persistentStoreCoordinator is automatically set to the persistentStoreCoordinator of the parent contex
[self.privateContext setPersistentStoreCoordinator:coordinator];
[self.managedObjectContext setParentContext:self.privateContext];
以下是在框架示例中使用的完整代码-
NSURL *modelURL = [[NSBundle bundleForClass:[self class]] URLForResource:@"TVModelSDK" withExtension:@"momd"];
NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
NSAssert(mom, @"Failed to initialize mom from URL: %@", modelURL);
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
/// DONOT set coordinator for managed Context !
// [moc setPersistentStoreCoordinator:coordinator];
[self setManagedObjectContext:moc];
[self setPrivateContext:[[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]];
/// when you set parentContext, the persistentStoreCoordinator is automatically set to the persistentStoreCoordinator of the parent contex
[self.privateContext setPersistentStoreCoordinator:coordinator];
[self.managedObjectContext setParentContext:self.privateContext];
谢谢“persistentStoreCoordinator自动设置为父上下文的persistentStoreCoordinator”您能详细说明一下吗?如果我有3个上下文A->B->C->persistent store,并且我刚刚插入了一些数据并想要保存它,我应该在A上调用save,在块内调用save-on-B,然后在另一个嵌套块内调用save-on-C吗?谢谢!“persistentStoreCoordinator自动设置为父上下文的persistentStoreCoordinator”您能详细说明一下吗?如果我有3个上下文A->B->C->persistent store,并且我刚刚插入了一些数据并想要保存它,我应该在A上调用save,在块内调用save-on-B,然后在另一个嵌套块内调用save-on-C吗?