不要再坑人啦!NSOperation才是蘋果推薦使用的多線程技術!,nsoperation多線程

來源:互聯網
上載者:User

不要再坑人啦!NSOperation才是蘋果推薦使用的多線程技術!,nsoperation多線程

首先聲明一下,小湯我在實際開發中呢,實際上也是喜歡用GCD多一點,因為用起來感覺更方便。
但是,事實是什麼呢?
蘋果推薦使用的實際上是NSOperation,所以希望對此沒有研究的各位不要再在部落格中誤導他人了啊。
再換句話來說,蘋果開發使用的Objective-C語言就是物件導向的,她怎麼可能推薦大家使用基於C語言的面向過程開發的東西呢? (這句話只是吐槽,請勿噴…)

關於NSOperation

很多人,在部落格中說到NSOperation的時候呢,都會以它是iOS2.0推出的 這一點來強調後來iOS4.0推出的GCD才是蘋果推薦使用的多線程使用方式!
對於這一點,我覺得倒也不能全怪他們,因為很多人在開發中都是通過查資料來學習的,很少有願意看蘋果給出的 官方文檔 的。
事實上,蘋果在官方文檔中對此有專門的講到,NSOperation的確是iOS2.0推出的,那個時候的NSOperation是通過NSThread實現的,效率的確一般,但是!!

蘋果在iOS4.0推出GCD之後,又重寫了NSOperation,從此以後,NSOperation的底層就是由GCD實現的了!!

下面是官方文檔的原文:

(Operation queues usually provide the threads used to run their operations. In OS X v10.6 and later, operation queues use the libdispatch library (also known as Grand Central Dispatch) to initiate the execution of their operations. As a result, operations are always executed on a separate thread, regardless of whether they are designated as concurrent or non-concurrent operations. In OS X v10.5, however, operations are executed on separate threads only if their isConcurrent method returns NO. If that method returns YES, the operation object is expected to create its own thread (or start some asynchronous operation); the queue does not provide a thread for it.)
NSOperation和GCD的對比:
        蘋果不再限制線程的最大並發數。

也就是說,這對於使用GCD開發是不利的,因為GCD是無法控制線程的最大並發數的,而線程的並發數一旦過多…你懂得~

所以,綜上,蘋果實際上推薦我們開發過程中使用的是NSOperation,不是GCD!還請各位在開發中稍加註意哦~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.