iOS Development--Multithreading OC & (iv) thread communication

Source: Internet
Author: User

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.M3 //06-nsthread04-inter-thread communication4 //5 //Created by Apple on 14-6-23.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "YYViewController.h"Ten @interfaceYyviewcontroller () One@property (Weak, nonatomic) Iboutlet Uiimageview *IconView; A @end -  - @implementationYyviewcontroller the  -- (void) Viewdidload - { - [Super Viewdidload]; + } -  +-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event A { at  - //call the download method in a child thread to download the picture - [Self performselectorinbackground: @selector (download) Withobject:nil]; - } -  -   in  --(void) Download to { +     //1. Download the picture according to the URL -     //download pictures from the network theNsurl *urlstr=[nsurl urlwithstring:@"FDSF"]; *  $     //convert a picture to binary dataPanax NotoginsengNSData *data=[nsdata DATAWITHCONTENTSOFURL:URLSTR];//This line of work can be time-consuming -  the     //convert the data into pictures +UIImage *image=[UIImage Imagewithdata:data]; A   the     //2. Go back to the main thread to set the picture + [Self performselectoronmainthread: @selector (settingimage:) withobject:image Waituntildone:no]; - } $  $   -  - //set the display picture the-(void) Settingimage: (UIImage *) Image - {WuyiSelf.iconview.image=image; the } -  Wu @end

Code Listing 2:

1 //2 //YYVIEWCONTROLLER.M3 //06-nsthread04-inter-thread communication4 //5 //Created by Apple on 14-6-23.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "YYViewController.h"Ten #import<NSData.h> One  A @interfaceYyviewcontroller () -@property (Weak, nonatomic) Iboutlet Uiimageview *IconView; - @end the  - @implementationYyviewcontroller -  -- (void) Viewdidload + { - [Super Viewdidload]; + } A  at   --(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]; in } -  to   +-(void) Download - { the  *     //1. Download the picture according to the URL $     //download pictures from the networkPanax NotoginsengNsurl *urlstr=[nsurl urlwithstring:@"FDSF"]; -  the     //convert a picture to binary data +NSData *data=[nsdata DATAWITHCONTENTSOFURL:URLSTR];//This line of work can be time-consuming A  the     //convert the data into pictures +UIImage *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     //The second way -     //[Self.imageview performselector: @selector (setimage:) onthread:[nsthread Mainthread] Withobject:image Waituntildone:no];Wuyi  the     //The Third Way - [Self.iconview performselectoronmainthread: @selector (setimage:) withobject:image Waituntildone:no]; Wu } -  About  $ //set the display picture - //-(void) Settingimage: (UIImage *) Image - //{ - //Self.iconview.image=image; A //} +  the @end

iOS Development--Multithreading OC & (iv) 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.