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:
#import "CKViewController.h" @interface ckviewcontroller ()@property(Weak,nonatomic)Iboutlet Uiimageview*iconview;@end @implementation ckviewcontroller - (void) Viewdidload {[SuperViewdidload]; } -(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) Event {//Call the download method in a child thread to download the picture[ SelfPerformselectorinbackground:@selector(download) Withobject:Nil]; } -(void) Download {//1. Downloading pictures based on URLs //download pictures from the network Nsurl*urlstr=[Nsurlurlwithstring:@"FDSF"];//Convert the image to binary dataNSData *data=[nsdata DATAWITHCONTENTSOFURL:URLSTR];//This line of operation will be time consuming //Convert the data into pictures UIImage*image=[UIImageImagewithdata:data];//2. Set the picture back to the main thread[ SelfPerformselectoronmainthread:@selector(settingimage:) withobject:image Waituntildone:NO]; }//Set display picture-(void) Settingimage: (UIImage*) Image { Self. IconView. Image=image; }@end
Code Listing 2:
#import "CKViewController.h" #import <NSData.h> @interface ckviewcontroller ()@property(Weak,nonatomic)Iboutlet Uiimageview*iconview;@end @implementation ckviewcontroller - (void) Viewdidload {[SuperViewdidload]; } -(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) Event {//Call the download method in a child thread to download the picture[ SelfPerformselectorinbackground:@selector(download) Withobject:Nil]; } -(void) Download {//1. Downloading pictures based on URLs //download pictures from the network Nsurl*urlstr=[Nsurlurlwithstring:@"FDSF"];//Convert the image to binary dataNSData *data=[nsdata DATAWITHCONTENTSOFURL:URLSTR];//This line of operation will be time consuming //Convert the data into pictures UIImage*image=[UIImageImagewithdata:data];//2. Set the picture back to the main thread //First way//[Self Performselectoronmainthread: @selector (settingimage:) withobject:image Waituntildone:no]; //Second way //[Self.imageview Performselector: @selector (setimage:) onthread:[nsthread Mainthread] Withobject:image Waituntildone:no]; //Third Way[ Self. IconViewPerformselectoronmainthread:@selector(setimage:) withobject:image Waituntildone:NO]; }//Set display picture//-(void) Settingimage: (UIImage *) Image//{ //Self.iconview.image=image; //} @end
iOS Development multithreading-Inter-thread communication