1. What is called inter-threading communication
In 1 processes, threads often do not exist in isolation and require frequent communication between multiple threads
2, 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
3. Example of inter-thread communication
Uiimageview Download Image This example, the main thread to open a sub-thread to download the picture, when the picture is downloaded and then back to the main thread to update the display image, such a process is a process of communication between the threads.
4. Common methods for nsthread communication between Threads
First Kind
-(void) Performselectoronmainthread: (SEL) Aselector withobject: (id) arg waituntildone: (BOOL) wait;
//second type -(void) Performselector: (SEL) aselector onthread: (Nsthread *) THR Withobject: (ID) arg waituntildone: ( BOOL) wait;
5, using Nsthread Inter-threaded communication
1- (void) Viewdidload2 {3 [Super Viewdidload];4 5 //use multithreading to download pictures6 [Nsthread detachnewthreadselector: @selector (downloadimage) totarget:self Withobject:nil];7 }8 9- (void) DownloadimageTen { OneNSLog (@"Start Download Image%@", [Nsthread CurrentThread]); A - //Download the method - //1. URL network resources theNsurl *url = [Nsurl urlwithstring:@"http://pic12.nipic.com/20110124/4814485_135921152102_2.jpg"]; - - //2. OC Native method, all data on the network is passed in a "binary or string" way - //in future development, almost no, because the performance is too poor +NSData *data =[NSData Datawithcontentsofurl:url]; - + //3. Create an image based on downloaded data AUIImage *image =[UIImage Imagewithdata:data]; at -NSLog (@"complete the download image%@", [Nsthread CurrentThread]); - - //To update the UI, you need to do it on the main thread - //Waituntildone: Whether to wait for SetImage: Method End - [Self performselectoronmainthread: @selector (setimage:) withobject:image Waituntildone:no]; in}
The above code is to use Nsthread multithreading way to implement sub-thread download picture, update the UI in the main thread operation
The above code is problematic, you should add an auto-release pool to multithreading
6, using GCD to complete the inter-threaded communication
1- (void) Viewdidload2 {3 [Super Viewdidload];4 5 //Download Image6 [self downloadimage];7 }8 9- (void) DownloadimageTen { One //1. Global Parallel Queue Adispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0); - - //2. Asynchronous Tasks theDispatch_async (Queue, ^{ -NSLog (@"Start Download Image%@", [Nsthread CurrentThread]); - - //Download the method + //1. URL network resources -Nsurl *url = [Nsurl urlwithstring:@"http://img.pconline.com.cn/images/upload/upc/tx/wallpaper/1208/30/c0/13398570_1346308990114_800x600.jpg "]; + A //2. OC Native method, all data on the network is passed in a "binary or string" way at //in future development, almost no, because the performance is too poor -NSData *data =[NSData Datawithcontentsofurl:url]; - - //3. Create an image based on downloaded data -UIImage *image =[UIImage Imagewithdata:data]; - inNSLog (@"complete the download image%@", [Nsthread CurrentThread]); - to //4. Notify the Host column to update the UI + //to the Home column, add an asynchronous task, update the UI -Dispatch_async (Dispatch_get_main_queue (), ^{ theSelf.image =image; * }); $ });Panax Notoginseng}
Compared with the Nsthread method, the problem of automatic free pool is not considered, and the operation of asynchronous task is placed in block code, which is relatively centralized and readable.
Multithreading _05_ inter-thread communication NSTHREAD/GCD in iOS