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