Objective c NSOperation子类中的performSelector
我在网上其他任何地方都找不到答案,所以如果有任何帮助,我将不胜感激 我正在创建一个系统,通过该系统,我可以检索NSOperation任务的结果,我知道具体的子类(如NSInvocation)无法完成该任务 我有一个NSOperation子类(引擎),它按照惯例是抽象的,必须进行扩展以实现函数Objective c NSOperation子类中的performSelector,objective-c,key-value-observing,nsoperation,nsoperationqueue,performselector,Objective C,Key Value Observing,Nsoperation,Nsoperationqueue,Performselector,我在网上其他任何地方都找不到答案,所以如果有任何帮助,我将不胜感激 我正在创建一个系统,通过该系统,我可以检索NSOperation任务的结果,我知道具体的子类(如NSInvocation)无法完成该任务 我有一个NSOperation子类(引擎),它按照惯例是抽象的,必须进行扩展以实现函数-main,包括要执行的代码体 发动机包含以下初始化功能,其工作只是记录选择器和选择器所属的对象。它还为属性isFinished注册一个千伏观察员: -(id)initWithCallbackSelector
-main
,包括要执行的代码体
发动机包含以下初始化功能,其工作只是记录选择器和选择器所属的对象。它还为属性isFinished
注册一个千伏观察员:
-(id)initWithCallbackSelector:(SEL)theSelector inObject:(id)theObject
在我的observeValueForKeyPath:ofObject:change:context:
函数中,我想这样调用回调函数:
NSLog(@"Some debug text to ensure this function is being called", nil);
[theObject performSelector:theSelector withObject:someData afterDelay:0];
整个过程如下:
aViewController启动引擎的扩展——比方说任务,方法是调用以下命令并将其添加到操作队列中
TheTask* TT = [[TheTask alloc] initWithCallbackSelector:
@selector(resultHandler:) inObject:theObject];
一切似乎都按预期运行,没有任何错误或异常。但是当执行到达对象:change:context:的observeValueForKeyPath:of时,实际上不会调用回调。我是Obj-C新手,所以我不能完全确定我对这种线程类型的理解是否正确
以下是完整的代码:
-(id)initWithCallbackSelector:(SEL)theSelector inObject:(id)theObject{
if([self init]){
self.selectorsParentObject = theObject;
self.selectorToCallWhenFinished = theSelector;
[self addObserver:self forKeyPath:@"isFinished" options:NSKeyValueObservingOptionNew context:NULL];
return self;
}
return nil;
}
-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)theObject change:(NSDictionary*)theChange context:(void*)theContext{
if([keyPath isEqualToString:@"isFinished"]){
NSLog(@"activity is finished with change: %@", theChange);
NSLog(@"target object: %@", self.selectorsParentObject);
NSLog(@"target selector: %@", NSStringFromSelector(self.selectorToCallWhenFinished));
//[self performSelectorOnMainThread:self.selectorToCallWhenFinished withObject:self.resultData waitUntilDone:NO];
[self.selectorsParentObject performSelector:@selector(selectorToCallWhenFinished) withObject:self.resultData afterDelay:0];
}
}
感谢您的帮助 您的
NSOperation
可能正在后台线程上运行。如果该线程消失,或者该线程无法启动其运行循环,那么对performSelector:withObject:afterDelay:
的调用将不会触发。您注释掉了对performSelectorOnMainThread:…
的调用。这有用吗
您可能应该在主线程上运行此命令,或者使用
性能选择器:withObject:
(没有后延迟:
)运行此命令performSelector:withObject:
不需要运行循环。正如Rob所建议的,代码是在后台线程中运行的,调用对象:更改:上下文:observeValueForKeyPath:ofObject:change:context:
我最初更改了代码,以便使用[self-performSelectorOnMainThread:@selector(runCallback)with-object:nil waitUntilDone:NO]在主线程上启动选择器代码>
但在本例中,主线程原来是任务,并且由于任务不拥有选择器
而引发异常。为了纠正这个问题,我创建了一个额外的函数-runCallback
,并从
-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)theObject change:(NSDictionary*)theChange context:(void*)theContext{
if([keyPath isEqualToString:@"isFinished"]){
NSLog(@"activity is finished with change: %@", theChange);
NSLog(@"target object: %@", self.selectorsParentObject);
NSLog(@"target selector: %@", NSStringFromSelector(self.selectorToCallWhenFinished));
[self performSelectorOnMainThread:@selector(runCallback) withObject:nil waitUntilDone:NO];
//[self performSelectorOnMainThread:self.selectorToCallWhenFinished withObject:self.resultData waitUntilDone:NO];
//[self.selectorsParentObject performSelector:@selector(selectorToCallWhenFinished) withObject:self.resultData afterDelay:0];
}
}
在-runCallback
中:
-(void)runCallback{
[self.selectorsParentObject performSelector:self.selectorToCallWhenFinished withObject:self.resultData afterDelay:0];
}
这将使用正确的数据调用任务中的选择器。
感谢您的参与:)谢谢您的提示,您的直觉证明是正确的-我将发布我所做的更改:)