Iosnsoperation Dependency Relationship

Source: Internet
Author: User

Nsoperation dependencies, just like you and your girlfriend, need each other the same. Very interesting.

#import "ViewController.h"


@interface viewcontroller ()


@end


@implementation Viewcontroller


-(void) viewdidload {

[superviewdidload];


// operation Dependent

// The time-consuming operation of processing pictures is performed in a child thread

nsblockoperation *blockop = [nsblockoperation Blockoperationwithblock: ^{

NSLog(@ "Blockop download picture thread =%@", [NsthreadCurrentThread]);

}];

nsblockoperation *blockop1 = [nsblockoperation Blockoperationwithblock: ^{

NSLog(@ "BlockOp1 adds a watermark to a picture thread =%@", [Nsthread CurrentThread]);

}];

nsblockoperation *blockop2 = [nsblockoperation Blockoperationwithblock: ^{

[nsthreadsleepfortimeinterval:2.0];

NSLog(@ "BlockOp2 save picture thread =%@", [ Nsthreadcurrentthread]);

}];

// Add dependencies to BlockOp1 to make blockOp1 in Blcokop Execute after execution finishes

[blockOp1 adddependency: Blockop]; // that is, after the download is finished, add a watermark to the image and save the picture. A dependent relationship

[blockOp2 adddependency: blockOp1];

// Create a queue (throw in the queue what you want to do--something like Asynchronous in GCD, concurrency, multiple threads turned on)

nsoperationqueue *queue = [[nsoperationqueuealloc] Init ];

// add to queue

[Queue addoperation: Blockop];

[Queue addoperation: blockOp1];

[Queue addoperation: blockOp2];

// Set the maximum number of simultaneous operations in the queue, that is, the current queue is executed at the same time by several threads, typically allowing the maximum number of concurrent 2 or 3

[Queue setmaxconcurrentoperationcount:3];

    //

for (int i =0; i< ; i++) {

nsblockoperation *blockopp = [nsblockoperationblockoperationwith Block: ^{

NSLog(@ "Blockopp i =%d thread =%@", i,[nsthread currentthread ]);

}];

[Queue addoperation: Blockopp];

}

// refresh UI operation dependencies must be performed in the main thread

nsblockoperation *blocopmain = [nsblockoperation Blockoperationwithblock: ^{

NSLog(@ "Blockopmain refresh UI display picture , thread =%@" , [nsthreadcurrentthread]);

}];

// The dependencies of the two operations, cross-queue

[Blocopmain adddependency: blockOp2];

// home Row

[[nsoperationqueuemainqueue]addoperation: Blocopmain] ;

}

@end


Iosnsoperation Dependency Relationship

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.