我使用一个和在这个块内执行一些工作NSBlockOperation
,持久存储丢失了它的持久存储。
以下代码:
- (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]);
}
}];
}
记录下来:
托管对象上下文是 < NSManagedObjectContext: 0xa46ff10 >
----->删除上下文的持久存储有1个存储
操作块中的托管对象上下文是 < NSManagedObjectContext: 0xa46ff10 >
操作块中的持久存储协调器是 < NSPersistentStoreCoordinator: 0xa1a45e0 >
操作块中删除上下文的持久存储有 0 个存储
*由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“此 NSPersistentStoreCoordinator 没有持久存储。它无法执行保存操作。
我不确定为什么会突然丢失存储,这可能是因为它在我设置为使用内存存储的测试套件中运行,并且内存不与NSOperationQueue
执行块的它共享吗?