我無法在網絡上的其他任何地方找到答案,所以任何幫助將不勝感激。NSOperation子類performSelector
我綁定創建一個系統,我可以檢索NSOperation任務的結果,我明白不能由具體的子類,如NSInvocation完成。
我有一個子類的NSOperation(TheEngine),它是按照慣例抽象和必須擴展到實現該功能-main
,以包括代碼以執行該主體。
TheEngine包含以下功能初始化他們的工作很簡單,就是注意theSelector
和theObject
選擇所屬。它還會註冊一個KV觀察員財產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啓動延伸TheEngine - 可以說TheTask通過調用以下內容並將其添加到操作隊列中。
TheTask* TT = [[TheTask alloc] initWithCallbackSelector:
@selector(resultHandler:) inObject:theObject];
一切似乎按預期運行,沒有任何錯誤或例外。但是當執行到達observeValueForKeyPath:ofObject:change:context:
時,實際上不會調用回調。我是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];
}
}
任何幫助表示讚賞!
感謝您的提示,你的預感變成了正確的 - 我會發布我所做的更改:) – aktar