Communication between threads and threads

Source: Internet
Author: User

Communication between threads and threads

We usually use multiple threads in iOS development, such as NSThread, GCD, and NSOperation. We usually load or download data in the Self-thread, therefore, it is inevitable to send the obtained data to the main thread for display or operation. below is the communication between the basic threads. Project Preparation: 1. create an Xcode project, create an image View in storyboard, set the constraints and the size of the image view control, and associate it with viewController (@ property (weak, nonatomic) IBOutlet UIImageView * imageview;) properties; 2. the image downloaded from the sub-thread is then displayed on the image view. Type 1: NSThread

 

-(Void) thread {// create a string NSString * str = @ "http://www.xiami.com/images/collect/802/2/10023802_1330239346.jpg"; [self supplied mselectorinbackground: @ selector (loadImage :) withObject: str];}-(void) loadImage :( NSString *) str {NSURL * url = [NSURL URLWithString: str]; NSData * data = [NSData dataWithContentsOfURL: url]; UIImage * image = [UIImage imageWithData: data]; NSLog (@ "loadImage -----> % @", [NSThread currentThread]);/*** this method returns to the main thread and runs * @ param updateUI: run the update ui Method */[self initialize mselectoronmainthread: @ selector (updateUI :) withObject: image waitUntilDone: NO];}-(void) updateUI :( UIImage *) image {self. imageview. image = image ;}

When the thread method is triggered, the performSelectorInBackground: withObject: ----> method starts a subthread, executes the loadImage: Method in the subthread, downloads the image in the loadImage, and then calls performselecw.mainthread: withObject; return to the main thread and assign the downloaded image to imageView in updateUI;

Type 2: GCD
// Call gcd
-(Void) gcd {NSString * str = @ "http://www.xiami.com/images/collect/802/2/10023802_1330239346.jpg ";
// The following block uses self to form a circular reference, and uses _ weak to modify weakSelf to place circular references. // The controller has a strong reference to the block, if self is used in the block, the block also has a strong reference to the Controller; _ weak typeof (self) weakSelf = self;/*** starts an asynchronous task, * obtain a global queue and download images from the global queue * enable an asynchronous task. In this asynchronous task, obtain a queue column and update the ui in the queue column; */dispatch_async (dispatch_get_global_queue (0, 0), ^ {NSURL * url = [NSURL URLWithString: str]; NSData * data = [NSData dataWithContentsOfURL: url]; UIImage * image = [UIImage imageWithData: data]; dispatch_async (dispatch_get_main_queue (), ^ {weakSelf. imageview. image = image ;});});}

 

In this example, two simple asynchronous tasks are used to combine the global queue and main queue columns for downloading and updating the ui. In the third case, NSoperation creates a file that inherits NSOperation, DownLoadOperation. h
// DownLoadOperation. h // communication between threads /// Created by two good or bad on 16/2/21. // Copyright©Qinakun. all rights reserved. // # import <Foundation/Foundation. h> # import <UIKit/UIKit. h> typedef void (^ FinishBlock) (UIImage * image); @ interface DownLoadOperation: NSOperation // image address @ property (nonatomic, copy) NSString * imageUrl; @ property (nonatomic, copy) FinishBlock finishBlock; @ end

 

DownLoadOperation. m
//// DownLoadOperation. m // communication between threads /// Created by two good three bad on 16/2/21. // Copyright©Qinakun. all rights reserved. // # import "DownLoadOperation. h "@ implementation DownLoadOperation-(void) main {@ autoreleasepool {// 1. 1. download NSURL * url = [NSURL URLWithString: self. imageUrl]; // 1. 2. load the image NSData * imageData = [NSData dataWithContentsOfURL: url]; // 1. 3. convert the binary of the image we obtained from the network to UIImage * image = [UIImage imageWithData: imageData]; if (self. finishBlock) {self. finishBlock (image) ;}}@ end

The code in the controller is as follows:

-(Void) operation {DownLoadOperation * operation = [DownLoadOperation new]; operation. imageUrl = @ "http://www.xiami.com/images/collect/802/2/10023802_1330239346.jpg"; // use block, operation. finishBlock = ^ (UIImage * image) {_ weak typeof (self) weakSelf = self; [[NSOperationQueue mainQueue] addOperationWithBlock: ^ {weakSelf. imageview. image = image ;}] ;};}

  

 

 

Related Article

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.