长话短说,我厌倦了与NSManagedObjectContext
相关的荒谬并发规则(或者说,它完全缺乏对并发性的支持,倾向于爆炸或者如果尝试分享时做其他不正确的事情跨线程的NSManagedObjectContext
),并试图实现线程安全变体。使核心数据线程安全
基本上我所做的就是建立追踪,它创建的线程,然后所有的方法调用映射回该线程的子类。这样做的机制是稍微令人费解,但它的关键是,我有一些辅助的方法,如:
- (NSInvocation*) invocationWithSelector:(SEL)selector {
//creates an NSInvocation for the given selector
NSMethodSignature* sig = [self methodSignatureForSelector:selector];
NSInvocation* call = [NSInvocation invocationWithMethodSignature:sig];
[call retainArguments];
call.target = self;
call.selector = selector;
return call;
}
- (void) runInvocationOnContextThread:(NSInvocation*)invocation {
//performs an NSInvocation on the thread associated with this context
NSThread* currentThread = [NSThread currentThread];
if (currentThread != myThread) {
//call over to the correct thread
[self performSelector:@selector(runInvocationOnContextThread:) onThread:myThread withObject:invocation waitUntilDone:YES];
}
else {
//we're okay to invoke the target now
[invocation invoke];
}
}
- (id) runInvocationReturningObject:(NSInvocation*) call {
//returns object types only
[self runInvocationOnContextThread:call];
//now grab the return value
__unsafe_unretained id result = nil;
[call getReturnValue:&result];
return result;
}
...然后子类以下类似的模式实现了NSManagedContext
接口:
- (NSArray*) executeFetchRequest:(NSFetchRequest *)request error:(NSError *__autoreleasing *)error {
//if we're on the context thread, we can directly call the superclass
if ([NSThread currentThread] == myThread) {
return [super executeFetchRequest:request error:error];
}
//if we get here, we need to remap the invocation back to the context thread
@synchronized(self) {
//execute the call on the correct thread for this context
NSInvocation* call = [self invocationWithSelector:@selector(executeFetchRequest:error:) andArg:request];
[call setArgument:&error atIndex:3];
return [self runInvocationReturningObject:call];
}
}
...然后我有一些代码,就像去测试它:
- (void) testContext:(NSManagedObjectContext*) context {
while (true) {
if (arc4random() % 2 == 0) {
//insert
MyEntity* obj = [NSEntityDescription insertNewObjectForEntityForName:@"MyEntity" inManagedObjectContext:context];
obj.someNumber = [NSNumber numberWithDouble:1.0];
obj.anotherNumber = [NSNumber numberWithDouble:1.0];
obj.aString = [NSString stringWithFormat:@"%d", arc4random()];
[context refreshObject:obj mergeChanges:YES];
[context save:nil];
}
else {
//delete
NSArray* others = [context fetchObjectsForEntityName:@"MyEntity"];
if ([others lastObject]) {
MyEntity* target = [others lastObject];
[context deleteObject:target];
[context save:nil];
}
}
[NSThread sleepForTimeInterval:0.1];
}
}
所以基本上,我旋转起来一些线程针对上面的切入点,他们跑了domly创建和删除实体。这几乎按照它应该的方式工作。
的问题是,调用obj.<field> = <value>;
时,每隔一段时间一个线程会得到一个EXC_BAD_ACCESS
。我不清楚问题是什么,因为如果我在调试器中打印obj
,一切看起来都不错。有什么问题可能会有什么建议(,除了Apple建议不要继承NSManagedObjectContext的事实)以及如何解决这个问题?
P.S.我知道GCD和NSOperationQueue
和其他技术通常用于“解决”这个问题。这些都不是我想要的。我正在寻找的是NSManagedObjectContext
,它可以自由,安全且直接地被任意数量的线程使用,以查看和更改应用程序状态,而无需任何外部同步。
问题是您在不同于上下文的线程上操作属性,因此可能与该上下文中的其他操作(包括保存和删除)同时操作?您可以尝试重写setSomeNumber,setAnotherNumber,setAString以在上下文线程上运行,并查看是否会影响结果。 – paulmelnikow
是的,似乎已经稳定它。所以现在的问题是,我该如何创建一个动态注入线程安全属性设置器实现的NSManagedObject子类? – aroth
我得到了setter注入的东西工作。它比'NSManagedObjectContext'更改更令人费解。但重要的是它的工作原理。如果有人有兴趣,我会分享相关部分的代码。 – aroth