IOS Nsoperation Learning

Source: Internet
Author: User

Why do you use Nsoperation? Why not GCD?

In my opinion the advantage of nsoperation is controllable, can be disconnected at any time, suspend thread

1Nsblockoperation *operationa = [Nsblockoperation blockoperationwithblock:^{2          for(inti =0; i< -; i++) {3NSLog (@"-operation-Download Picture-%@", [Nsthread CurrentThread]);4         }5     }];6 7Operationa.completionblock = ^{8NSLog (@"--Start to download the second picture--%@", [Nsthread CurrentThread]);9     };Ten  OneNSLog (@"--What are you doing now,--%@?", [Nsthread CurrentThread]); A  -Nsoperationqueue *queue =[[Nsoperationqueue alloc] init]; -[Queue Addoperation:operationa];

Nsoperation Adding thread dependencies

Nsinvocationoperation *operation1 = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (Operationa :)Object: nil]; Nsinvocationoperation*operation2 = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (operationb:)Object: nil]; Nsblockoperation*operation3 = [Nsblockoperation blockoperationwithblock:^{         for(inti =0; i<Ten; i++) {NSLog (@"nsblockoperation3=--1-----%@", [Nsthread CurrentThread]);    }    }]; [Operation3 Addexecutionblock:^{         for(inti =0; i<Ten; i++) {NSLog (@"nsblockoperation3=--2-----%@", [Nsthread CurrentThread]);    }    }]; //adding dependencies cannot depend on each other[Operation1 Adddependency:operation2];    [Operation2 Adddependency:operation3]; //Create NsoperationqueueNsoperationqueue *queue =[[Nsoperationqueue alloc] init];    [Queue Addoperation:operation1];    [Queue Addoperation:operation2]; [Queue Addoperation:operation3];

IOS Nsoperation Learning

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.