iOS Development Nsoperation III: Operational dependency and monitoring and inter-thread communication

Source: Internet
Author: User

One: Operation dependency and monitoring

#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller/** 1:nsoperation use: 1: Create a queue First nsoperationqueue: If you do not create a Queue direct encapsulation task, the default is to execute the task serially in the current thread, and its queue is divided into two primary and non-home columns, the same as the master queue in the hosts column and the GCD [ Nsoperationqueue Mainqueue], the queue created by Alloc init, which defaults to serial and concurrency, is concurrent by default and can be determined by specifying the maximum number of concurrent queues or the home column, which is 0, and does nothing by default. Default to 1 for serial queue, greater than 1 for concurrent Queue 2: Encapsulating tasks with nsoperation subclasses, Nsblockoperation,nsinvercation, or inheriting nsoperation overriding the Main method wrapper task, (There are also quick package tasks: 1:[queue addoperationwithblock:<#^ (void) block#>]; 2: You can also append a task after a task: [OP4 Addexecutionblock], when the number of tasks in an operation is greater than 1, the child thread is turned on, and its child threads are likely to be open to an asynchronous thread that might be the main thread and possibly the current one. 3: Can also add operation listening: Op3.completionblock, when the OP3 task is completed, it will enter this block to perform task 4: can also implement operation dependency: can cross queue dependency, but can not cycle dependent adddependency) 3: Add the operation to the queue , [queue Addoperation:op1]; This method executes the [queue start] by default; * */-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{    //1. Create a queueNsoperationqueue *queue =[[Nsoperationqueue alloc]init]; Nsoperationqueue*queue2 =[[Nsoperationqueue alloc]init]; //2. Encapsulation OperationNsblockoperation *OP1 = [Nsblockoperation blockoperationwithblock:^{NSLog (@"1---%@", [Nsthread CurrentThread]);        }]; Nsblockoperation*OP2 = [Nsblockoperation blockoperationwithblock:^{NSLog (@"2---%@", [Nsthread CurrentThread]);        }]; Nsblockoperation*OP3 = [Nsblockoperation blockoperationwithblock:^{NSLog (@"3---%@", [Nsthread CurrentThread]);        }]; Nsblockoperation*OP4 = [Nsblockoperation blockoperationwithblock:^{NSLog (@"4---%@", [Nsthread CurrentThread]);        }]; //Operation MonitoringOp3.completionblock = ^{NSLog (@"++++ sir, come and see me------%@", [Nsthread CurrentThread]);        }; [OP4 Addexecutionblock:^{NSLog (@"5---%@", [Nsthread CurrentThread]);        }]; [OP4 Addexecutionblock:^{NSLog (@"6---%@", [Nsthread CurrentThread]);        }]; [OP4 Addexecutionblock:^{NSLog (@"7---%@", [Nsthread CurrentThread]);            }]; //Add Operation Dependency//Note: You cannot cycle dependencies.//can be cross-queue dependent[OP1 ADDDEPENDENCY:OP4];//[OP4 ADDDEPENDENCY:OP1];[Op2 ADDDEPENDENCY:OP3]; //add action to queue[Queue Addoperation:op1];    [Queue ADDOPERATION:OP2];    [Queue ADDOPERATION:OP3];    [Queue2 ADDOPERATION:OP4]; }@end

Two: inter-thread communication

#import "ViewController.h"@interfaceViewcontroller () @property (weak, nonatomic) Iboutlet Uiimageview*ImageView;@end@implementationViewcontroller-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{[self Combie];}-(void) download{//http://s15.sinaimg.cn/bmiddle/4c0b78455061c1b7f1d0e        //1. Open sub-thread download picture//1.1 Non-home rowNsoperationqueue *queue =[[Nsoperationqueue alloc]init]; //1.2 Package OperationNsblockoperation *download = [Nsblockoperation blockoperationwithblock:^{Nsurl*url = [Nsurl urlwithstring:@"http://s15.sinaimg.cn/bmiddle/4c0b78455061c1b7f1d0e"]; NSData*imagedata =[NSData Datawithcontentsofurl:url]; UIImage*image =[UIImage Imagewithdata:imagedata]; NSLog (@"Download---%@", [Nsthread CurrentThread]); //3. Updating the UI[[Nsoperationqueue Mainqueue] addoperationwithblock:^{self.imageView.image=image; NSLog (@"UI---%@", [Nsthread CurrentThread]);            }];        }]; //2. Adding operations to the queue[queue addoperation:download];}/*1. Download picture 1 2. Download Picture 2 3. Merging pictures*/-(void) combie{//1. Create a queueNsoperationqueue *queue =[[Nsoperationqueue alloc]init]; __block UIImage*Image1; __block UIImage*Image2; //2 package operation, download picture 1Nsblockoperation *download1 = [Nsblockoperation blockoperationwithblock:^{Nsurl*url = [Nsurl urlwithstring:@"http://s15.sinaimg.cn/bmiddle/4c0b78455061c1b7f1d0e"]; NSData*imagedata =[NSData Datawithcontentsofurl:url]; Image1=[UIImage Imagewithdata:imagedata]; NSLog (@"Download---%@", [Nsthread CurrentThread]);        }]; //3 package operation, download picture 2Nsblockoperation *download2 = [Nsblockoperation blockoperationwithblock:^{Nsurl*url = [Nsurl urlwithstring:@"http://www.027art.com/feizhuliu/UploadFiles_6650/201109/2011091718442835.jpg"]; NSData*imagedata =[NSData Datawithcontentsofurl:url]; Image2=[UIImage Imagewithdata:imagedata]; NSLog (@"Download---%@", [Nsthread CurrentThread]);        }]; //4. Encapsulate the action of merging picturesNsblockoperation *combie = [Nsblockoperation blockoperationwithblock:^{        //4.1 Open ContextUigraphicsbeginimagecontext (Cgsizemake ( $, $)); //4.2 drawing 1[Image1 Drawinrect:cgrectmake (0,0, -, $)]; //4.3 drawing 2[Image2 Drawinrect:cgrectmake ( -,0, -, $)]; //4.4 Get pictures based on contextUIImage *image =Uigraphicsgetimagefromcurrentimagecontext (); //4.5 Closing the contextUigraphicsendimagecontext (); //7. Updating the UI[[Nsoperationqueue Mainqueue] addoperationwithblock:^{self.imageView.image=image; NSLog (@"UI----%@", [Nsthread CurrentThread]);            }];        }]; //5. Setting up Dependencies[Combie Adddependency:download1];        [Combie adddependency:download2]; //6. Adding actions to the queue[Queue addoperation:download2];    [Queue Addoperation:download1]; [Queue Addoperation:combie];}@end

-2.4 Nsoperation for inter-thread communication

(1) Open sub-thread download picture

"' OBJC

1. Create a queue

Nsoperationqueue *queue = [[Nsoperationqueue alloc]init];

2. Encapsulate the operation and add it to the queue in a convenient way

[Queue addoperationwithblock:^{

3. Download the image in the block

Nsurl *url = [Nsurl urlwithstring:@ "http://news.51sheyuan.com/uploads/allimg/111001/133442IB-2.jpg"];

NSData *data = [NSData Datawithcontentsofurl:url];

UIImage *image = [UIImage imagewithdata:data];

NSLog (@ "Download picture operation--%@", [Nsthread CurrentThread]);

4. Go back to the main thread to refresh the UI

[[Nsoperationqueue Mainqueue] addoperationwithblock:^{

Self.imageView.image = image;

NSLog (@ "Refresh UI Action---%@", [Nsthread CurrentThread]);

}];

}];

```

iOS Development Nsoperation III: Operational dependency and monitoring and 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.