What is the difference between NSInvocationOperation and NSBlockOperation

给你一囗甜甜゛ 提交于 2019-12-20 10:32:37

问题


There are three operation classes in Foundation Framework(NSOperation, NSInvocationOperation and NSBlockOperation).

I already read the concurrency programming guide but I did't understand exactly what is the difference between these three classes. Please help me.


回答1:


NSBlockOperation exectues a block. NSInvocationOperation executes a NSInvocation (or a method defined by target, selector, object). NSOperation must be subclassed, it offers the most flexibility but requires the most code.

NSBlockOperation and NSInvocationOperation are both subclasses of NSOperation. They are provided by the system so you don't have to create a new subclass for simple tasks.

Using NSBlockOperation and NSInvocationOperation should be enough for most tasks.


Here is a code example for the use of all three that do exactly the same thing:

// For NSOperation subclass
@interface SayHelloOperation : NSOperation
@end

@implementation SayHelloOperation

- (void)main {
    NSLog(@"Hello World");
}

@end

// For NSInvocationOperation
- (void)sayHello {
    NSLog(@"Hello World");
}


- (void)startBlocks {
    NSBlockOperation *blockOP = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"Hello World");
    }];
    NSInvocationOperation *invocationOP = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(sayHello) object:nil];
    SayHelloOperation *operation = [[SayHelloOperation alloc] init];

    NSOperationQueue *q = [[NSOperationQueue alloc] init];
    [q addOperation:blockOP];
    [q addOperation:invocationOP];
    [q addOperation:operation];
}



回答2:


NSOperation is the base class. All operations extend NSOperation. But it is an abstract class, meaning that you need to subclass it and implement the code that performs the actual operation.

For simple cases, this (making your own subclass) is not necessary, however.

If you just want to pass a message to an object, you can use NSInvocationOperation. This is an implementation of NSOperation that can be parameterized with a selector and a target object.

If you just want to call one more blocks, you can use NSBlockOperation. This is an implementation of NSOperation that can be parameterized with a block to call.




回答3:


As par Apple doc NSInvocationOperation and NSBlockOperation Both are subclass of NSOperation

NSInvocationOperation Class

The NSInvocationOperation class is a concrete subclass of NSOperation that manages the execution of a single encapsulated task specified as an invocation. You can use this class to initiate an operation that consists of invoking a selector on a specified object. This class implements a non-concurrent operation


NSBlockOperation Class

The NSBlockOperation class is a concrete subclass of NSOperation that manages the concurrent execution of one or more blocks. You can use this object to execute several blocks at once without having to create separate operation objects for each. When executing more than one block, the operation itself is considered finished only when all blocks have finished executing.

Blocks added to a block operation are dispatched with default priority to an appropriate work queue. The blocks themselves should not make any assumptions about the configuration of their execution environment.



来源:https://stackoverflow.com/questions/18632271/what-is-the-difference-between-nsinvocationoperation-and-nsblockoperation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!