+ [NSThread detachNewThreadSelector:toTarget:withObject:]和 - [NSObject performSelectorInBackground:withObject:]有什么区别? [英] What is the difference between +[NSThread detachNewThreadSelector:toTarget:withObject:] and -[NSObject performSelectorInBackground:withObject:]?

查看:118
本文介绍了+ [NSThread detachNewThreadSelector:toTarget:withObject:]和 - [NSObject performSelectorInBackground:withObject:]有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它们似乎执行一个合理类似的任务:启动一个新的线程,快速,轻松地执行该选择器。但是有什么区别吗?也许关于内存管理?

They seem to perform a reasonably similar task: launching a new thread that performs that selector quickly and easily. But are there any differences? Maybe with regards to memory management?

推荐答案

两者都是一样的。

在iOS和Mac OS X v10.5及更高版本中,所有对象都能够生成一个新线程并使用它来执行其中一个方法。 performSelectorInBackground:withObject:方法创建一个新的分离线程,并使用指定的方法作为新线程的入口点。例如,如果你有一个对象(由变量myObj表示),并且该对象有一个名为doS​​omething的方法,你想在后台线程中运行,你可以使用下面的代码:

In iOS and Mac OS X v10.5 and later, all objects have the ability to spawn a new thread and use it to execute one of their methods. The performSelectorInBackground:withObject: method creates a new detached thread and uses the specified method as the entry point for the new thread. For example, if you have some object (represented by the variable myObj) and that object has a method called doSomething that you want to run in a background thread, you could could use the following code to do that:

[myObj performSelectorInBackground:@selector(doSomething) withObject:nil];

调用此方法的效果与调用detachNewThreadSelector时相同:toTarget: withObject:方法的NSThread与当前对象,选择器和参数对象作为参数。使用默认配置立即生成新线程并开始运行。在选择器中,您必须像配置任何线程一样配置线程。例如,你需要设置一个自动释放池(如果你没有使用垃圾收集),并配置线程的运行循环,如果你计划使用它。有关如何配置新线程的信息

The effect of calling this method is the same as if you called the detachNewThreadSelector:toTarget:withObject: method of NSThread with the current object, selector, and parameter object as parameters. The new thread is spawned immediately using the default configuration and begins running. Inside the selector, you must configure the thread just as you would any thread. For example, you would need to set up an autorelease pool (if you were not using garbage collection) and configure the thread’s run loop if you planned to use it. For information on how to configure new threads

这篇关于+ [NSThread detachNewThreadSelector:toTarget:withObject:]和 - [NSObject performSelectorInBackground:withObject:]有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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