在打开ARC的NSOperation中保存NSManagedObjectContext时,我发现了一些问题。没有ARC之前一切都很好。保存时总是给出EXC_BAD_ACCESS。代码看起来像这样:
//on the main thread
-(void)someFunc
{
array = ... //fetching an array of entities from a core data
for(SomeEntity * obj in array)
{
NSSomeOperation * op = [[NSSomeOperation alloc] initWithValue:[obj someField]];
//start an operation
}
}
//NSSomeOperation implementation
//...
- (void)main {
//some code
NSError * error = nil;
[mainContext lock];
if (![mainContext save:&error]) { //<--- HERE EXC_BAD_ACCESS
//process error
}
[mainContext unlock];
//some code
}
//...
使用[mainContext setRetainsRegisterdObjects:YES]和objectWithID并不能解决此问题。
EXC_BAD_ACCESS(代码=1)
EXC_BAD_ACCESS(代码=13)
-[__NSCFType contextDidSave:]: unrecognized selector sent to instance 0x7fc5c505d940
An observer of NSManagedObjectContextDidSaveNotification illegally threw an exception.
Objects saved = {
inserted = "{(\n)}";
updated = "{(\n <SomeEntity: 0x7fc5c55b6220> (entity: SomeEntity; id: 0x7fc5c5052b20 ... )}"; }
and exception = -[__NSCFType contextDidSave:]: unrecognized selector sent to instance 0x7fc5c505d940 with userInfo = (null)
我使用单独的托管对象上下文,并在此NSOperation中获取我的托管对象。
也许这与核心数据错误或ARC有关?也许ARC会清理一些必须保存的对象?
因为,没有ARC,一切都很好,一切都正常。当我打开ARC-EXC_BAD_ACCESS时。
有人知道为什么会发生吗?