Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which one is easier to use? GCD or NSOperation?

I am currently using GCD. However, I've heard that NSOperation is actually a higher level program. It's far more complex though.

In GCD, do something at background is simply use this helper function I created:

+(void)doForeGround:(void (^)())block
{
    dispatch_async(dispatch_get_main_queue(), ^{
        block();
    });
}

+(void)doBackground:(void (^)())block
{

    //DISPATCH_QUEUE_PRIORITY_HIGH
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW,0), ^{
    //dispatch_async(dispatch_get_global_queue(-2,0), ^{
        block();
    });
}

-(void)doBackGround:(void (^)())block onComletion:(void (^)())onCompletion
{
    [BGHPTools doBackground:^{
        block();
        [BGHPTools doForeGround:^{
            onCompletion();
        }];
    }];
}

Will doing it with NSOperation be simpler?

Am I missing something? How would I do the same thing at NSoperation?

like image 878
Anonymous White Avatar asked Oct 19 '12 02:10

Anonymous White


People also ask

What is an advantage of using NSOperation vs GCD?

Benefits of NSOperation. You can Pause, Cancel, Resume an NSOperation . With Grand Central Dispatch, you no longer have control or insight into the execution of that task. The NSOperation API is more flexible in that respect, giving the developer control over the operation's life cycle.

Which is the best of GCD Nsthread or NSOperationQueue?

There is no generic "best" out of the three.

What is difference between GCD and operation queue?

GCD tends to be simpler to work with for simple tasks you just need to execute and forget. Operations provide much more functionality when you need to keep track of a job or maintain the ability to cancel it. If you're just working with methods or chunks of code that need to be executed, GCD is a fitting choice.

What is the difference in creating the thread using GCD and using Nsthread?

From what I read online, GCD seems to do the exact same thing as basic threads (POSIX, NSThreads etc.) while adding much more technical jargon ("blocks"). It seems to just overcomplicate the basic thread creation system (create thread, run function).


1 Answers

You can do similar things with NSOperation as you're doing with GCD. The main difference is that NSOperation provides additional functionality.

For example:

  • NSOperation has a -cancel method. Dispatch queues have no concept of cancellation; all blocks enqueued on a queue will run to completion.
  • NSOperationQueue has a maximumConcurrentOperationCount property, which you can use (for example) to only allow 3 operations to run at a time. Dispatch queues have no such concept; they are either serial, allowing only 1 block at a time, or concurrent, allowing as many as libdispatch thinks advisable based on CPU usage and availability.
  • An NSOperation can have a dependency on other NSOperations, allowing you to defer execution of a particular operation until all of its dependencies have run. Other operations will be allowed to "jump ahead" in the queue while the dependent operation is waiting. Dispatch queues are always dequeued in strictly FIFO order. (You can somewhat imitate dependencies using the dispatch_group API, but that's really targeted at a different kind of problem.)

Now, if you're not using any of those features, GCD works just fine. There's nothing wrong with using GCD, per se. It's just that NSOperation provides a convenient wrapper for some additional nice features.

Here's how you'd rewrite your examples above using NSOperationQueue:

+(void)doForeground:(void (^)())block
{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        NSLog(@"I'm running on the main thread!");
        block();
    }];
}

+(void)doBackground:(void (^)())block
{
    // Note; rather than allocating a new NSOperationQueue every time, you could
    // allocate the queue once and just refer to that queue. For simplicity, I'll
    // skip that here.
    [[NSOperationQueue new] addOperationWithBlock:^{
        NSLog(@"I'm running asynchronously on whatever thread NSOperationQueue wants!");
        block();
    }];
}

-(void)doBackground:(void (^)())block onCompletion:(void (^)())onCompletion
{
    [[NSOperationQueue new] addOperationWithBlock:^{
        block();
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            onCompletion();
        }];
    }];
}
like image 97
BJ Homer Avatar answered Sep 30 '22 01:09

BJ Homer