Objective-C 中的线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/925468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Threading in Objective-C
提问by Raju
Are there threads in Objective C? If so, how are they declared and used?
Objective C 中有线程吗?如果是这样,它们是如何声明和使用的?
If anybody knows about multithreading in Objective C, please share with me.
如果有人知道 Objective C 中的多线程,请与我分享。
Thanks and regards.
感谢致敬。
回答by monowerker
An easy way to just spin off a method in a new thread is to use.
在新线程中分离方法的一种简单方法是使用。
+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgumenton NSThread. If you aren't running garbage collected you need to set up your own autorelease pool.
+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument上NSThread。如果您没有运行垃圾收集,则需要设置自己的自动释放池。
Another easy way if you just don't want to block the main thread is to use.
如果您不想阻塞主线程,另一种简单的方法是使用。
- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)argon NSObject
- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg在 NSObject
Depending on what type of concurrency you are after you should also take a look at NSOperationthat can give you free locking so you can share it between several threads among other things.
根据您所追求的并发类型,您还应该看看NSOperation它可以为您提供免费锁定,以便您可以在多个线程之间共享它。
回答by Tom Jefferys
If you're developing using Cocoa (ie for the mac or iphone), you have access to the NSThreadclass, which can be used for multithreading. Googling for NSThreadwill find you the API.
如果您使用 Cocoa 进行开发(即用于 mac 或 iphone),您可以访问NSThread该类,该类可用于多线程。谷歌搜索NSThread会找到你的 API。
You can declare it like using:
您可以像使用一样声明它:
NSThread *mythread = [[NSThread alloc] initWithTarget:target selector:selector object:argument];
Where target and selector is the object and selector you want to start a thread with, and argument is an argument to send to the selector.
其中 target 和 selector 是您想要启动线程的对象和选择器,argument 是要发送到选择器的参数。
Then use [mythread start] to start it.
然后使用[mythread start]启动它。
回答by Mihir Mehta
Yes there are threading concepts in Objective C. and there are multiple way to achieve multi threading in objective C.
是的,Objective C 中有线程概念。Objective C 中有多种实现多线程的方法。
1> NSThread
1> NSThread
[NSThread detachNewThreadSelector:@selector(startTheBackgroundJob) toTarget:self withObject:nil];
This will create a new thread in the background. from your main thread.
这将在后台创建一个新线程。从你的主线程。
2> Using performSelector
2>使用 performSelector
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
will perform UI task on your main thread if you call this from background thread... You can also use
如果你从后台线程调用它,将在你的主线程上执行 UI 任务......你也可以使用
[self performSelectorInBackground:@selector(abc:) withObject:obj];
Which will create a background thread.
这将创建一个后台线程。
3> Using NSoperation
3>使用NS操作
4> Using GCD
4>使用 GCD
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self callWebService];
dispatch_async(dispatch_get_main_queue(), ^{
[self updateUI];
});
});
Will callWebServicein background thread and once it's completed. It will updateUIin main thread.More about GCD
将callWebService在后台线程中,一旦完成。它将updateUI在主线程中。更多关于 GCD
This is almost all way of multithreading that is used in iOS. hope this helps.
这几乎是 iOS 中使用的所有多线程方式。希望这可以帮助。
回答by Abizern
You could also look into NSOperation
你也可以看看NSOperation
To see an example of this, have a look at Drew McCormack's post on MacResearch.
要查看此示例,请查看 Drew McCormack 在MacResearch上的帖子。
回答by James Williams
Before going to far with stuff like detachNewThreadSelector: be sure to check out Apple's official documentation. For a high-level overview of options (including operation queues, dispatch queues, and such), there's the Concurrency Programming Guide. And, for a look at lower-level (and less recommended) threading, there's the Threading Programming Guide.
在深入了解 detachNewThreadSelector 之类的内容之前:请务必查看 Apple 的官方文档。有关选项(包括操作队列、调度队列等)的高级概述,请参阅并发编程指南。而且,要查看较低级别(且不太推荐)的线程,请参阅Threading Programming Guide。
You definitely don't want to just start spinning up threads without reading what Apple has to say on the subject first. They've done a lot of work with stuff like GCDto make it easier and safer to write concurrent programs.
您绝对不想在没有首先阅读 Apple 就该主题所说的内容的情况下开始旋转线程。他们在GCD 之类的东西上做了很多工作,使编写并发程序更容易、更安全。

