iOS Development multithreading-Inter-thread communication
A simple explanation
Inter-thread communication: In 1 processes, threads often do not exist in isolation, and frequent communication between multiple threads is required
The embodiment of inter-thread communication
1 Threads pass data to another 1 threads
After performing a specific task in 1 threads, go to another 1 thread to continue the task
Common methods for inter-thread communication
-(void) Performselectoronmainthread: (SEL) Aselector withobject: (ID) arg waituntildone: (BOOL) wait;
-(void) Performselector: (SEL) aselector onthread: (Nsthread *) THR Withobject: (ID) arg waituntildone: (BOOL) wait;
Example of inter-thread communication – image download
Code Listing 1:
1//2//YYVIEWCONTROLLER.M 3//06-nsthread04-inter-thread communication 4//5//Created by Apple on 14-6-23. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYViewController.h" @interface Yyviewcontroller () One @property (weak, nonatomic) Iboutlet Uiimageview *iconview;12 @end13 @implementation YYViewController15-(void) ViewDidLoad17 {[Super viewdidload];19}20 21 -(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event22 {23 24//Call download method in a child thread download picture [self Performselec Torinbackground: @selector (download) withobject:nil];26}27 (void) download31 {32//1. Download image from URL 33//from the network Download photo nsurl *urlstr=[nsurl urlwithstring:@ "FDSF"];35 36//Convert picture to binary data PNs NSData *data=[nsdata datawithcontents ofurl:urlstr];//This line of work will take 38 39//To convert the data into pictures UIImage *image=[uiimage imagewithdata:data];41 42//2. Go back to the main thread to set the picture [Self Performselectoronmainthread: @selector (settingimage:) withobject:image waituntildone:no];44}45 46 47 48//Set display picture (void) Settingimage: (UIImage *) image50 {Wuyi self.iconview.image=image;52}53 @end
Code Listing 2:
1//2//YYVIEWCONTROLLER.M 3//06-nsthread04-inter-thread communication 4//5//Created by Apple on 14-6-23. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYViewController.h" #import <nsdata.h>11 @interface Yyviewcontroller () @property (weak, n onatomic) Iboutlet uiimageview *iconview;14 @end15 @implementation YYViewController17-(void) VIEWDIDLOAD19 {20 [Super viewdidload];21}22-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *) Event25 {26//Call download in a child thread method download Picture [Self performselectorinbackground: @selector (download) withobject:nil];29}30-(void) Download33 {34 35//1. Download picture according to URL 36//download picture from the network PNs nsurl *urlstr=[nsurl urlwithstring:@ "FDSF"];38 39//Convert the picture to binary data-NSD ATA *data=[nsdata datawithcontentsofurl:urlstr];//This line of operation will be time consuming 41 42//Convert data into pictures UIImage *image=[uiimage Imagewithda TA:DATA];44 45//2. Go back to the main thread to set the picture 46///[Self Performselectoronmainthread: @selector (settingimage:) wIthobject:image waituntildone:no];48 49//Second way//[Self.imageview Performselector: @selector (setimage:) onthr Ead:[nsthread Mainthread] Withobject:image waituntildone:no];51 52//Third Way [Self.iconview Performselectoronmainth READ: @selector (setimage:) withobject:image waituntildone:no];54}55 56 57//Set display picture//-(void) Settingimage: (UIImage *) image59//{60//self.iconview.image=image;61//}62 @end
iOS Development-multithreading-inter-thread communication