IOS 8: "Go" nsoperation notes

Source: Internet
Author: User

Source Address: http://fann.im/blog/2014/02/23/nsoperation/

Multithreaded programming under IOS has both Nsoperation and Grand Central Dispatch (GCD), which simply notes some of the nsoperation's use.

NSOperationQueueEquivalent to an action pool, operation is automatically executed according to First-in-first-out (FIFO) policy when added. Operation is typically added to a custom queue that applies a global share, which avoids blocking the execution of the main thread.

Some simple multithreaded requirements do not need to use nsoperation this big guy, NSInvocationOperation it is very convenient:

Nsinvocationoperation*op = [[nsinvocationoperation span class= "n" >alloc initwithtarget:self selector: @selector  (anywork ) object:nil]; [operationqueue addoperation:op ; - (voidanywork {//perform any work in Operation}       

Nsinvocationoperation is not very convenient to share operations, if an operation will be required in many places, you can do a nsoperation sub-class encapsulation:

@implementation CustomOperation- (void)main{ //perform any work in operation}@end

This subclass only implements main methods, which are easier to share than nsinvocationoperation. If you need to do more detailed functions of the operation, such as state control, you need more complex inheritance implementation, see Afurlconnectionoperation, in this case not inherit main , but inherit the implementation, and start cancel then through the KVO Manually control the switching of the operating state.

Nsoperation can set dependencies, a operation depends on the B operation to complete, then you can set B to a dependency [A addDependency:B]; .

If there is no dependency between the operations, but need to do after the completion of all operations to do some work, there are two solutions, one is to add all operations for the aftermath of the dependency, so that all other operations after the completion of the operation will be executed, this method is more rigid, or can be used KVO listening queue operand, After the operation is complete after the queue operation is empty, do the aftercare work:

[OperationqueueAddobserver:SelfForkeypath:@ "Operationcount"Options:0Context:Nil];-(void)Observevalueforkeypath:(NSString*)KeyPathOfobject:(Id)ObjectChange(Nsdictionary*)ChangeContext(void*)Context{If(Object==Operationqueue&&[KeyPathIsequaltostring:@ "Operationcount" ] {if  (operationqueue. Operationcount == 0 {//any Final operation. } } else {[super observevalueforkeypath:keypath ofObject :object change:change context:context}}             /span>                 
Nsoperation or GCD

Nsoperation and GCD can meet multi-threaded needs, then which is the choice? When-to-use nsoperation vs. GCD Views:

Always use the highest-level abstraction available-to-you, and drop-to-lower-level abstractions when measurement show s that they is needed.

Nsoperation provides more features than GCD, such as Operation execution status, pause and cancel operations execution, such as dependencies between operations, such as controlling the number of operations that the operation queue can perform at the same time.

GCD is easy to use compared to nsoperation, and the system overhead is very small.

The simple small operation in the actual project is direct GCD, flexible and convenient, large-scale control complex operation or through nsoperation as well, also can enjoy the convenience of advanced API.

IOS 8: "Go" nsoperation notes

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.