Ios multi-thread operations (11)-advanced NSOperation operations

Source: Internet
Author: User

Ios multi-thread operations (11)-advanced NSOperation operations
NSOperation can call the start method to execute tasks. By default, NSOperation is executed synchronously. If you add NSOperation to NSOperationQueue, the system automatically performs operations in the queue asynchronously. The operation can be canceled in NSOperationQueue. The pause and resume cancellation functions are as follows:

- (void)cancelAllOperations;
You can also call the NSOperation cancel Method to cancel a single operation.
- (void)cancel;
The queue has an attribute "suincluded". You can set "YES" or "NO" for this BOOL value to pause and resume operations. The Operation also has a priority. By setting the NSOperation priority in the queue, you can change the operation execution priority. NSOperation has an attribute queuePriority, which can be used to set the priority, values of priority include-8L, NSOperationQueuePriorityLow =-4L, NSOperationQueuePriorityNormal = 0, NSOperationQueuePriorityHigh = 4, priority = 8 NSOperation, which has a very common attribute, this attribute is defined in the header file as follows:
@property (copy) void (^completionBlock)(void) NS_AVAILABLE(10_6, 4_0);

With this attribute, we can monitor the operation. After the operation is completed, the block code in the completionBlock will be executed, for example:
// Create a new block Operation NSBlockOperation * op = [NSBlockOperation blockOperationWithBlock: ^ {[NSThread sleepForTimeInterval: 3.0]; NSLog (@ "% @, come here ", [NSThread currentThread]);}]; // create an operation queue NSOperationQueue * queue = [[NSOperationQueue alloc] init]; // Add the operation to the queue [queue addOperation: op]; // listen for this operation. After the operation is completed, the Code [op setCompletionBlock: ^ {[NSOperationQueue mainQueue] addOperationWithBlock: ^ {NSLog (@ "% @, here ", [NSThread currentThread]) ;}];

The effect is as follows:


No matter what operations are performed, the block code will always be executed at the end! Dependencies can be set between NSoperation to ensure the execution sequence. For example:
NSBlockOperation * op1 = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "User Logon % @", [NSThread currentThread]);}]; NSBlockOperation * op2 = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "% @", [NSThread currentThread]);}]; NSBlockOperation * op3 = [NSBlockOperation blockOperationWithBlock: ^ {[NSThread sleepForTimeInterval: 1.0f]; NSLog (@ "Download % @", [NSThread currentThread]) ;}]; NSBlockOperat Ion * op4 = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "Update UI % @", [NSThread currentThread]);}]; /* "dependency between specified tasks"-synchronization tasks used by GCD (without threads) NSOperation will open the thread, but the operation execution sequence can still be ensured, and the concurrency efficiency will be better! Dependency, which is a cross-queue * // before payment, You need to log on to [op2 addDependency: op1]; // before downloading, you need to pay [op3 addDependency: op2]; // complete the download before updating the UI [op4 addDependency: op3]; // Note: When specifying the dependency, be sure not to see Circular dependency // waitUntilFinished = NO, asynchronous YES is synchronous [self. queue addOperations: @ [op1, op2, op3] waitUntilFinished: NO]; // UI update operations should be scheduled by the main queue column [[NSOperationQueue mainQueue] addOperation: op4]; NSLog (@ "come here ");


The execution result is as follows:

Related Article

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.