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:
////YYVIEWCONTROLLER.M//06-nsthread04-inter-thread communication////Created by Apple on 14-6-23.//Copyright (c) 2014 itcase. All rights reserved.//#import"YYViewController.h"@interface Yyviewcontroller () @property (weak, nonatomic) Iboutlet Uiimageview*IconView, @end @implementation yyviewcontroller- (void) viewdidload{[Super Viewdidload];}-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{//call the download method in a child thread to download the picture[Self performselectorinbackground: @selector (download) Withobject:nil];} -(void) download{//1. Download the picture according to the URL//download pictures from the networkNsurl *urlstr=[nsurl urlwithstring:@"FDSF"]; //convert a picture to binary dataNSData *data=[nsdata DATAWITHCONTENTSOFURL:URLSTR];//This line of work can be time-consuming//convert the data into picturesUIImage *image=[UIImage Imagewithdata:data]; //2. Go back to the main thread to set the picture[Self performselectoronmainthread: @selector (settingimage:) withobject:image waituntildone:no];}//set the display picture-(void) Settingimage: (UIImage *) image{Self.iconView.image=image;} @end
Code Listing 2:
////YYVIEWCONTROLLER.M//06-nsthread04-inter-thread communication////Created by Apple on 14-6-23.//Copyright (c) 2014 itcase. All rights reserved.//#import"YYViewController.h"#import<NSData.h>@interface Yyviewcontroller () @property (weak, nonatomic) Iboutlet Uiimageview*IconView, @end @implementation yyviewcontroller- (void) viewdidload{[Super Viewdidload];}-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event{//call the download method in a child thread to download the picture[Self performselectorinbackground: @selector (download) Withobject:nil];} -(void) download{//1. Download the picture according to the URL//download pictures from the networkNsurl *urlstr=[nsurl urlwithstring:@"FDSF"]; //convert a picture to binary dataNSData *data=[nsdata DATAWITHCONTENTSOFURL:URLSTR];//This line of work can be time-consuming//convert the data into picturesUIImage *image=[UIImage Imagewithdata:data]; //2. Go back to the main thread to set the picture//The first way//[Self performselectoronmainthread: @selector (settingimage:) withobject:image Waituntildone:no]; //The second way//[Self.imageview performselector: @selector (setimage:) onthread:[nsthread Mainthread] Withobject:image Waituntildone:no]; //The Third Way[Self.iconview performselectoronmainthread: @selector (setimage:) withobject:image waituntildone:no];}//set the display picture//-(void) Settingimage: (UIImage *) Image//{//Self.iconview.image=image;//}@end
iOS Development multithreading-Inter-thread communication