本文介绍了NSOperation子类中的performSelector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网络上的其他任何地方都找不到答案,因此我们将不胜感激.

I couldn't find an answer anywhere else on the net so any help would be appreciated.

我想创建一个系统,通过该系统我可以检索NSOperation任务的结果,据我了解,NSInvocation之类的具体子类无法完成该任务.

I am tying to create a system whereby I can retrieve the results of an NSOperation task, which I understand cannot be done by concrete subclasses such as NSInvocation.

我有一个NSOperation子类( TheEngine ),该子类按照惯例是抽象的,必须进行扩展以实现功能-main,以包括要执行的代码体.

I have an NSOperation subclass (TheEngine) which is abstract by convention and must be extended to implement the function -main, to include the body of code to execute.

TheEngine 包含以下初始化函数,其作用只是注意选择器所属的theSelectortheObject.它还为属性isFinished注册一个KV观察者:

TheEngine contains the following initialisation function whose job is simply to note theSelector and theObject the selector belongs to. It also registers a KV observer for the property isFinished :

-(id)initWithCallbackSelector:(SEL)theSelector inObject:(id)theObject

在我的observeValueForKeyPath:ofObject:change:context:函数中,我想像这样调用回调函数:

In my observeValueForKeyPath:ofObject:change:context: function I would like to call the callback function like so:

NSLog(@"Some debug text to ensure this function is being called", nil);
[theObject performSelector:theSelector withObject:someData afterDelay:0];

整个过程如下:

aViewController 触发 TheEngine 的扩展-通过调用以下代码并将其添加到操作队列来说 TheTask .

aViewController fires up an extension of TheEngine - lets say TheTask by calling the following and adding it to an operations queue.

TheTask* TT = [[TheTask alloc] initWithCallbackSelector:
    @selector(resultHandler:) inObject:theObject];

一切似乎都按预期运行,完全没有任何错误或异常.但是,当执行到达observeValueForKeyPath:ofObject:change:context:时,实际上不会调用该回调.我是Obj-C的新手,所以我不确定我对这种线程类型的理解是否正确.

Everything seems to run as expected without any errors or exceptions at all. But when execution reaches the observeValueForKeyPath:ofObject:change:context: the callback is not actually called. I'm new to Obj-C, so I'm not entirely sure if my understanding of this type of threading is correct.

这是完整的代码:

-(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:...的呼叫.这项工作成功了吗?

Your NSOperation is likely running on a background thread. If that thread goes away, or if that thread fails to pump its run loop, then your call to performSelector:withObject:afterDelay: will not fire. You commented out a call to performSelectorOnMainThread:.... Did this work?

您可能应该在主线程上运行此程序,或者使用performSelector:withObject:(不包含afterDelay:)运行它. performSelector:withObject:不需要运行循环.

You probably should be running this on the main thread or running this with performSelector:withObject: (without the afterDelay:). performSelector:withObject: does not require a run loop.

这篇关于NSOperation子类中的performSelector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 00:19