如何在 Objective-C 中等待线程完成
我正在尝试使用我在某处下载的类中的方法。该方法在程序继续执行的同时在后台执行。我不想让程序继续执行直到该方法完成。我该怎么做?
I'm trying to use a method from a class I downloaded somewhere. The method executes in the background while program execution continues. I do not want to allow program execution to continue until this method finishes. How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是使用 GCD 的另一种方法:
Here's another way to do it using GCD:
使用 NSOperationQueue,如下所示
(根据记忆,请原谅任何小错误——你会得到基本的想法):
Use an NSOperationQueue, like this
(from memory so forgive any minor errors -- you'll get the basic idea):
我的第一反应是不做你的建议。我之前使用的技术是为线程提供原始对象(位于主线程上)中的方法的选择器。当第二个线程启动时,主线程继续执行,但在显示屏上显示某种忙碌指示器。这允许用户交互在需要时继续。
当第二个线程结束时,就在关闭之前,它会调用主线程上的选择器。然后,选择器引用的方法会从显示中删除繁忙指示器,并告诉主线程进行更新,获取第二个线程生成的任何数据。
我已成功地将其用于访问 Web 服务(在第二个线程上)的应用程序,然后在返回数据后更新显示而不锁定它。这使得用户体验变得更好。
My first inclination is to not do what you are suggesting. The technique I've used before is to give the thread a selector to a method in the originating object (which is on the main thread). When the second thread is started, the main thread keeps executing, but puts up a busy indicator of some sort on the display. This allows user interaction to continue if required.
When the second thread ends, just before it shuts down, it calls the selector on the main thread. The method that the selector references then removes the busy indicator from the display and tells the main thread to update, picking up whatever data the second thread has generated.
I've used this successfully for an app which accesses a web service (on the second thread) and then updates the display once data is returned without locking it. This makes the user experience much nicer.
对于这种情况,我通常使用 NSCondition 类。
For such cases I usually using NSCondition class.
多线程同步的几种技术方式,如
NSConditionLock
(互斥锁)、NSCondition
(信号量)。但都是其他语言的通用编程知识(java.. .) 除了 Objective-c。我更喜欢引入runloop
(Cocoa中特别)来实现线程连接:Several technical ways to synchronize multi-threads, such as
NSConditionLock
(mutex-lock),NSCondition
(semaphore)。But they are common programming knowledge for other languages(java...) besides objective-c. I prefer to introducerun loop
(special in Cocoa) to implement thread join:我建议在您自己的方法中结束对类方法的调用,并在完成后设置一个布尔值。例如:
您希望如何处理等待取决于您:也许使用 [NSThread sleepForTimeInterval:1] 或类似的轮询,或者每个运行循环向自己发送一条消息。
I'd suggest wrapping up call to the class method in your own method, and set a boolean once it completes. For eg:
How you wish to handle waiting is up to you: Perhaps polling with [NSThread sleepForTimeInterval:1] or similar, or sending self a message each run loop.
我的技术是检查任务是否可以运行(如果后台线程已完成),如果无法运行,则我使用 GCD 在延迟后重试:
My technique is to check if the task can run (if the background thread is finished), and if it can't run then I use GCD to try again after a delay: