iOS Development multithreading-Inter-thread communication

Source: Internet
Author: User

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

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.