iOS Development multithreading-Inter-thread communication

Source: Internet
Author: User

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

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.