简体   繁体   English

iOS中的多线程

[英]Multithreading in ios

Need your help. 需要你的帮助。

In my application, i want to implement a background process which keeps running continuously and downloads the updated data and stores it in document folder. 在我的应用程序中,我想实现一个后台进程,该进程保持连续运行,并下载更新的数据并将其存储在文档文件夹中。

And my main thread should keep checking the document folder and display the updated data in view control. 而且我的主线程应继续检查文档文件夹并在视图控件中显示更新的数据。

The child thread should end once the view disappears. 一旦视图消失,子线程应结束。 and start again once the view appears. 并在视图出现后重新开始。

What is the best way to do it? 最好的方法是什么? NSThread or NSOperationQueue? NSThread或NSOperationQueue? What precautions are required? 需要采取哪些预防措施? I also have to access few variables of the class. 我还必须访问该类的几个变量。 So is should be thread safe. 因此应该是线程安全的。

Thanks in advance. 提前致谢。

Regards 问候


If you do not need to update a progress bar or something in iOS5 there is one great API method + sendAsynchronousRequest:queue:completionHandler: that allows you to run async download as a block inside NSOperationQueue. 如果您不需要更新进度条或iOS5中的某些功能,则可以使用一种很棒的API方法+ sendAsynchronousRequest:queue:completionHandler:它可以将异步下载作为NSOperationQueue中的一个块运行。 If not you should look into third party libs such as ASIHTTP request or https://github.com/AFNetworking/AFNetworking (probably better the last one) or you need to build you own download manager, not a simple task 如果没有,您应该研究第三方库,例如ASIHTTP请求或https://github.com/AFNetworking/AFNetworking (可能是最后一个更好),或者您需要构建自己的下载管理器,而不是简单的任务

there are two ways to do it.. 有两种方法可以做到。

First: You use NSOperationQueue which is a bit bulkier as it is build on GCD but does have some extra features. 首先:您使用NSOperationQueue,因为它是基于GCD构建的,所以有点笨重,但确实具有一些额外的功能。

Second: You use GCD (grand central dispatch) looking at requirements I would say GCD seems fine as you can easily access any thread(main or background) this would be slightly quicker. 第二:您使用GCD(大型中央调度)查看要求我会说GCD看起来不错,因为您可以轻松访问任何线程(主线程或后台线程),这会更快一些。

You can have a look at - (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg This method creates a new thread in your application, putting your application into multithreaded mode if it was not already. 您可以看一下- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg此方法在您的应用程序中创建一个新线程,如果尚未将其置于多线程模式,则将其置于多线程模式。 In your viewDidDisappear, you can stop the task when your view disappears 在viewDidDisappear中,您可以在视图消失时停止任务

From Apple Docs. 来自Apple Docs。 Apple encourages to investigate the alternative Mac OS X technologies for implementing concurrency. 苹果鼓励研究用于实现并发性的替代Mac OS X技术。 This is especially true if you are not already familiar with the design techniques needed to implement a threaded application. 如果您还不熟悉实现线程应用程序所需的设计技术,则尤其如此。 These alternative technologies simplify the amount of work you have to do to implement concurrent paths of execution and offer much better performance than traditional threads. 这些替代技术可简化实现并发执行路径的工作量,并提供比传统线程更好的性能。 https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/AboutThreads/AboutThreads.html#//apple_ref/doc/uid/10000057i-CH6-SW2 https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/AboutThreads/AboutThreads.html#//apple_ref/doc/uid/10000057i-CH6-SW2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM