Cat Learn iOS (53) nsoperation introduction of multi-threaded Network

Source: Internet
Author: User
<span id="Label3"></p><p><p>Cat share, must Boutique</p></p><p><p>Original articles, Welcome Reprint. Reprint please specify: Sanayu's blog<br>Address: http://blog.csdn.net/u013357243?viewmode=contents</p></p>A: Brief introduction of 1:nsoperation's work? steps to Use:<p><p>Multithreaded programming is also possible with nsoperation and Nsoperationqueue.</p></p><p><p>Nsoperation and nsoperationqueue The specific steps for multithreading:</p></p><p><p>(1) first encapsulate the action that needs to be performed into a Nsoperation object (subclass instance).</p></p><pre><pre><code>NSOperationQueue的作用:NSOperation可以调用start方法来执行任务,但默认是同步执行的如果将NSOperation添加到NSOperationQueue(操作队列)中,系统会自动异步执行NSOperation中的操作。</code></pre></pre><p><p>(2) the Nsoperation object is then added to the Nsoperationqueue.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs erlang">//这里是两个子类的方法后面详细介绍<span class="hljs-pp">- <span class="hljs-params">(void)</span>addOperation:<span class="hljs-params">(<span class="hljs-variable">NSOperation</span> *)</span>op;- <span class="hljs-params">(void)</span>addOperationWithBlock:<span class="hljs-params">(void (^)</span><span class="hljs-params">(void)</span>)block;</span></code></pre></pre><p><p>(3) the system will move the Nsoperationqueue in the nsoperation to take Out.</p></p><p><p>(4) put the operation of the removed Nsoperation package into the new Thread.</p></p><p><p>Sub-Class of 2.NSOperation</p></p><p><p>Nsoperation is an abstract class, and does not have the ability to encapsulate operations, it must make it a subclass of</p></p><p><p>There are 3 ways to use the Nsoperation class:</p></p><p><p>(1) nsinvocationoperation</p></p><p><p>(2) nsblockoperation</p></p><p><p>(3) Customize the subclass inheritance nsoperation, and implement the internal corresponding Method.</p></p>Three ways to use Nsoperation (subclass) 1:nsinvocationoperation<p><p>The simplest usage (in the main thread:)</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs objectivec">-(<span class="hljs-keyword">void</span>)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ <span class="hljs-comment">// 1.创建1个操作</span> NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:<span class="hljs-keyword">self</span> selector:<span class="hljs-keyword">@selector</span>(download) object:<span class="hljs-literal">nil</span>]; <span class="hljs-comment">// 2.开启操作</span> [operation start];}-(<span class="hljs-keyword">void</span>)download{ <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"download---%@"</span>, [<span class="hljs-built_in">NSThread</span> currentThread]);}</code></pre></pre><p><p></p></p><p><p>Async method (remember This)</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs objectivec">-(<span class="hljs-keyword">void</span>)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ <span class="hljs-comment">// 1.创建一个队列(非主队列)</span> NSOperationQueue *queue = [[NSOperationQueue alloc] init]; <span class="hljs-comment">// 2.创建1个操作</span> NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:<span class="hljs-keyword">self</span> selector:<span class="hljs-keyword">@selector</span>(download) object:<span class="hljs-literal">nil</span>]; <span class="hljs-comment">// 3 .将操作添加到队列(非主队列)————这一步内部相当于开启</span> [queue addOperation:operation];}-(<span class="hljs-keyword">void</span>)download{ <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"download---%@"</span>, [<span class="hljs-built_in">NSThread</span> currentThread]);}</code></pre></pre><p><p>Effect: Thread 3 (where The system automatically assigns us the most appropriate thread)<br></p></p><p><p>Note: the Action object is executed by default in the main thread (the previous one), and only new threads are opened when added to the Queue. That is, by default, if the action is not placed in the queue, it is performed synchronously. Operations are performed asynchronously only if the nsoperation is placed in a nsoperationqueue</p></p>2:nsblockoperation<p><p>The second is also very simple:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs objectivec">-(<span class="hljs-keyword">void</span>)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ <span class="hljs-comment">// 1.创建一个队列(非主队列)</span> NSOperationQueue *queue = [[NSOperationQueue alloc] init]; <span class="hljs-comment">// 2.创建1个操作</span> NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"download---%@"</span>, [<span class="hljs-built_in">NSThread</span> currentThread]); }]; <span class="hljs-comment">// 3 .将操作添加到队列(非主队列)————这一步内部相当于开启</span> [queue addOperation:operation];}</code></pre></pre><p><p>Effect:<br></p></p><p><p>There's also a Chinese notation to add a block directly to the Queue.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs objectivec">-(<span class="hljs-keyword">void</span>)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ <span class="hljs-comment">// 1.创建一个队列(非主队列)</span> NSOperationQueue *queue = [[NSOperationQueue alloc] init]; <span class="hljs-comment">// 2.将操作添加到队列(非主队列)————这一步内部相当于开启</span> [queue addOperationWithBlock:^{ <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"download---%@"</span>, [<span class="hljs-built_in">NSThread</span> currentThread]); }];}</code></pre></pre>3: Custom subclass Inherits nsoperation, implement internal corresponding method<p><p>This is not difficult, is to customize a nsoperation subclass, to implement his main method</p></p><p><p>How to define a custom class</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs objectivec"><span class="hljs-comment">//</span><span class="hljs-comment">// NYOperation.m</span><span class="hljs-comment">// NSOperation使用</span><span class="hljs-comment">//</span><span class="hljs-comment">// Created by apple on 15-5-27.</span><span class="hljs-comment">// Copyright (c) 2015年 znycat. All rights reserved.</span><span class="hljs-comment">//</span><span class="hljs-preprocessor">#import <span class="hljs-title">"NYOperation.h"</span></span><span class="hljs-class"><span class="hljs-keyword">@implementation</span> <span class="hljs-title">NYOperation</span></span>-(<span class="hljs-keyword">void</span>)main{ <span class="hljs-built_in">NSLog</span>(@<span class="hljs-string">"download---%@"</span>, [<span class="hljs-built_in">NSThread</span> currentThread]);}<span class="hljs-keyword">@end</span></code></pre></pre><p><p>Controller invocation notation</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cpp">-(<span class="hljs-keyword">void</span>)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ <span class="hljs-comment">// 1.创建一个队列(非主队列)</span> NSOperationQueue *<span class="hljs-built_in">queue</span> = [[NSOperationQueue alloc] init]; <span class="hljs-comment">// 2.创建1个操作</span> NYOperation *operation = [[NYOperation alloc]init]; <span class="hljs-comment">// 3 .将操作添加到队列(非主队列)————这一步内部相当于开启</span> [<span class="hljs-built_in">queue</span> addOperation:operation];}</code></pre></pre><p><p>Results:<br></p></p> <p><p>Cat Learn iOS (53) nsoperation introduction of multi-threaded Network</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.