Multi-threaded Nsoperation in iOS

Source: Internet
Author: User

In iOS, there are three ways to use multithreading, respectively: Nsthread, Nsoperation and Nsoperationqueue, GCD, in this section, the main explanation of the use of nsoperation.

Nsoperation and Nsoperationqueue This approach is actually to put nsoperation objects in a nsoperationqueue queue, and then start the operation in turn, similar to the use of the thread pool.

In the process of use, Nsoperation's operation uses its subclasses, respectively, Nsinvocationoperation and nsblockoperation, there is no essential difference between the two, but the latter to block the way to achieve, the use of relatively simple. Nsoperationqueue is primarily responsible for managing and executing all nsoperation objects and controlling the order of execution and dependencies between threads.

Below, start multi-threaded with nsoperation to get pictures from the network and refresh.

Nsinvocationoperation

Code

//VIEWCONTROLLER.M//aaaaaa////Created by Jerei on 15-11-8.//Copyright (c) 2015 jerehedu. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload];}#pragmaMark-click the button to open the thread download image-(ibaction) Click_invocationopreation_load: (UIButton *) Sender {Nsurl*url = [Nsurl urlwithstring:@"Http://www.jerehedu.com/images/temp/logo.gif"]; //Create a OperationNsinvocationoperation *operation = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector ( Loadimagewithurl:)Object: url]; //Add to Action queueNsoperationqueue *queue =[[Nsoperationqueue alloc] init]; [Queue addoperation:operation];}#pragmaMark-Get a picture by URL-(void) Loadimagewithurl: (Nsurl *) url{NSData*data =[NSData Datawithcontentsofurl:url]; UIImage*image =[UIImage Imagewithdata:data]; //back to the main thread update interfaceNsinvocationoperation *operation = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector ( Updateimageview:)Object: Image]; [[Nsoperationqueue Mainqueue] addoperation:operation];}#pragmaMark-Update Interface-(void) Updateimageview: (UIImage *) img{_imageview.image=img;}@end

Nsblockoperation

Code

//VIEWCONTROLLER.M//aaaaaa////Created by Jerei on 15-11-8.//Copyright (c) 2015 jerehedu. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload];}#pragmaMark-click the button to open the thread download image-(ibaction) Click_blockopreation_load: (UIButton *) Sender {//Create an action queueNsoperationqueue *operationqueue =[[Nsoperationqueue alloc] init]; //set maximum number of concurrent threadsOperationqueue.maxconcurrentoperationcount =5; //< method one > Create Operation//nsblockoperation *operation = [Nsblockoperation blockoperationwithblock:^{//        //request data based on URL//Nsurl *url = [Nsurl urlwithstring:@]Http://www.jerehedu.com/images/temp/logo.gif"];//[self loadimagewithurl:url];//    }];//    //    //Add to Queue//[Operationqueue addoperation:operation]; //< Method two > Create Operation[Operationqueue addoperationwithblock:^{        //request data based on URLNsurl *url = [Nsurl urlwithstring:@"Http://www.jerehedu.com/images/temp/logo.gif"];    [Self loadimagewithurl:url]; }];}#pragmaMark-Get a picture by URL-(void) Loadimagewithurl: (Nsurl *) url{NSData*data =[NSData Datawithcontentsofurl:url]; UIImage*image =[UIImage Imagewithdata:data]; //back to the main thread update interface[[Nsoperationqueue Mainqueue] addoperationwithblock:^{[Self updateimageview:image]; }];}#pragmaMark-Update Interface-(void) Updateimageview: (UIImage *) img{_imageview.image=img;}@end

Jerry Education
Source:http://www.cnblogs.com/jerehedu/
Copyright Notice: The copyright of this article belongs to cigarettes DeskJerry EducationSection Technology Co., Ltd. and blog Park are shared, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Technical Consultation:

Multi-threaded Nsoperation in iOS

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.