PerformSelector不工作 [英] PerformSelector not working

查看:140
本文介绍了PerformSelector不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从MyMethod调用MyThreadRun方法,如此

MyThreadRun method is invoked from MyMethod like this

NSArray* args = [NSArray arrayWithObjects:arg1, target, NSStringFromSelector(mySelector), nil];
NSThread* mythread= [[[NSThread alloc] initWithTarget:self selector: @selector(MyThreadRun:) object:args] autorelease];
[MyThreadRun start];

在MyThreadRun的末尾,我尝试调用已调用MyMethod的类中的函数来启动开头的线程,如下所示:

In the end of MyThreadRun, I try to invoke a function in the class which has called MyMethod to initiate the thread to begin with, like this:

NSObject* callbackTarget = [args objectAtIndex:1];
NSString* selector = [args objectAtIndex:2];
[callbackTarget performSelector:NSSelectorFromString(selector) withObject:calculationResult afterDelay:0];

我在选择器指向的方法上有一个断点,它永远不会被击中。

I have a break point on the method that selector is pointing at, and it is never hit.

如果我硬编码方法名称,就像这样

If I hard code the method name, like this

[callbackTarget updateWithResult:calculationResult] 

它工作正常。

有什么关于performSelector我需要知道的吗?

What is there I need to know about performSelector?

推荐答案

调用 performSelector:withObject:afterDelay:的上下文是罪魁祸首。这是正在发生的事情。

The context where performSelector:withObject:afterDelay: is getting invoked is the culprit. Here's what's going on.

performSelector ...系列的一些成员,就像这个一样,不立即执行选择器;它们在当前运行循环中排队调用,以便在fn返回之后发生,即运行循环的下一个循环。根据apple的说法:指定延迟为0并不一定会导致选择器立即执行。选择器仍然在线程的运行循环中排队并尽快执行。

Some members of the performSelector... family, like this one, don't perform the selector right away; they queue up an invocation on the current run loop, so that it happens after your fn returns, the next go-round of the run loop. According to apple: "Specifying a delay of 0 does not necessarily cause the selector to be performed immediately. The selector is still queued on the thread’s run loop and performed as soon as possible."

通常这很好并且预期。但是你的代码是在你手动启动的线程上调用它...而且这样的线程不会像主线程那样重复运行循环。他们调用在创建时指定的选择器,然后退出。所以:你的代码排队调用你的回调选择器,然后线程退出;并且它的运行循环被抛弃而没有运行...所以你的排队调用永远不会发生。

Normally this is fine and expected. But your code is calling it on a thread that you started manually... and such threads don't keep their run loop going repeatedly the way the main thread does. They invoke the selector specified at creation once, and exit. So: your code queues up an invocation of your callback selector, but then the thread exits; and its run loop is thrown away without ever running... so your queued invocation never happens.

你可能需要的是 performSelectorOnMainThread:withObject: waitUntilDone:,因为您可能希望回调发生在首先调用 MyMethod 方法的线程上,这可能是主线程。

What you probably need is performSelectorOnMainThread:withObject:waitUntilDone:, since you may want the callback to happen on the thread that invoked the MyMethod method in the first place, which is presumably the main thread.

更一般地说,线程化非常棘手。我强烈建议您查看 NSOperationQueue NSBlockOperation 以及相关技术 - 它们可以消除很多痛苦。

More generally, threading is very tricky stuff. I highly recommend checking out NSOperationQueue, NSBlockOperation, and related techniques - they can remove a great deal of the pain.

这篇关于PerformSelector不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆