[英]Strange Core-Data behaviour
I perform some work using an NSBlockOperation
and inside this block the persistent store loses its persistent stores. 我使用NSBlockOperation
执行一些工作,在此块中,持久性存储丢失了其持久性存储。
the following code: 以下代码:
- (void) eraseCoreData_ManagedObjectsInArray:(NSArray *) arrayOfManagedObjectsToDelete usingManagedContext:(NSManagedObjectContext *) managedObjectContext
{
NSLog(@"Managed object context is %@", managedObjectContext);
NSLog(@"----->Persistent store of the deletion context has %d stores", [managedObjectContext.persistentStoreCoordinator.persistentStores count]);
// add the operation to the queue with a block
[self.coreDataDeletionQueue addOperationWithBlock:^{
for (NSManagedObject *objectToDelete in arrayOfManagedObjectsToDelete) {
[managedObjectContext deleteObject:objectToDelete];
}
NSLog(@"Managed object context in operation block is %@", managedObjectContext);
NSLog(@"Persistence store coordinator in operation block is %@", managedObjectContext.persistentStoreCoordinator);
NSLog(@"Persistent store of the deletion context in operation block has %d stores", [managedObjectContext.persistentStoreCoordinator.persistentStores count]);
NSError *saveError = nil;
if (![managedObjectContext save:&saveError]) {
NSLog(@"Couldn't save Core-Data state for the deleted objects result: %@", [saveError localizedDescription]);
}
}];
}
logs this out: 将此注销:
Managed object context is < NSManagedObjectContext: 0xa46ff10 > 受管对象上下文为<NSManagedObjectContext:0xa46ff10>
----->Persistent store of the deletion context has 1 stores ----->删除上下文的持久存储区有1个存储区
Managed object context in operation block is < NSManagedObjectContext: 0xa46ff10 > 操作块中的托管对象上下文为<NSManagedObjectContext:0xa46ff10>
Persistence store coordinator in operation block is < NSPersistentStoreCoordinator: 0xa1a45e0 > 操作块中的持久性存储协调器为<NSPersistentStoreCoordinator:0xa1a45e0>
Persistent store of the deletion context in operation block has 0 stores 删除上下文在操作块中的永久存储有0个存储
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores. *由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“此NSPersistentStoreCoordinator没有持久存储。 It cannot perform a save operation.' 它无法执行保存操作。
I am not sure why this sudden lose of stores, could it be due to the fact that this is running in a test suite that I set to use an in memory store and the memory is not shared with this NSOperationQueue
that executes the blocks? 我不确定为什么突然丢失存储,是否可能是由于它运行在我设置为使用内存中存储的测试套件中,并且该执行块不与该NSOperationQueue
共享内存?
My guess you are using your code in the wrong manner. 我猜您使用的是错误的代码。
First, each thread must have its own context. 首先,每个线程必须具有自己的上下文。 So, inside the block operation you need to create a new context and use it within that block. 因此,在块操作内部,您需要创建一个新上下文并在该块中使用它。
NSManagedObjectContext *blockContext = [[NSManagedObjectContext alloc] init];
[blockContext setPersistentStoreCoordinator:[managedObjectContext persistentStoreCoordinator]];
Then, if you need to share objects within contexts, use NSManagedObjectID
and not NSManagedObject
. 然后,如果需要在上下文中共享对象,请使用NSManagedObjectID
而不是NSManagedObject
。 Before creating that block save the main context or you will have only temporary ids. 在创建该块之前,请保存主上下文,否则您将只有临时ID。 Otherwise use - (BOOL)obtainPermanentIDsForObjects:(NSArray *)objects error:(NSError **)error
to obtain permanent ids without saving. 否则请使用- (BOOL)obtainPermanentIDsForObjects:(NSArray *)objects error:(NSError **)error
来获取永久性ID而无需保存。
NSArray *objectIDs = [arrayOfManagedObjectsToDelete valueForKey:@"objectID"];
Within the block use - (NSManagedObject *)existingObjectWithID:(NSManagedObjectID *)objectID error:(NSError **)error
to retrieve the object you are interested in through blockContext
. 在块内使用- (NSManagedObject *)existingObjectWithID:(NSManagedObjectID *)objectID error:(NSError **)error
可以通过blockContext
检索您感兴趣的对象。
Your code could look like 您的代码可能看起来像
NSArray *objectIDs = [arrayOfManagedObjectsToDelete valueForKey:@"objectID"];
// add the operation to the queue with a block
[self.coreDataDeletionQueue addOperationWithBlock:^{
NSManagedObjectContext *blockContext = [[NSManagedObjectContext alloc] init];
[blockContext setPersistentStoreCoordinator:[managedObjectContext persistentStoreCoordinator]];
NSError* error = nil;
for (NSManagedObject *objectID in objectIDs) {
// some error handling here...
NSManagedObject* objectToDelete = [existingObjectWithID:objectID error:&error];
[blockContext deleteObject:objectToDelete];
}
if (![blockContext save:&error]) {
NSLog(@"Couldn't save Core-Data state for the deleted objects result: %@", [error localizedDescription]);
}
}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.