Giter Club home page Giter Club logo

asyncblockoperation's Introduction

AsyncBlockOperation

Create asynchronous operations using blocks.

NSBlockOperation's blockOperationWithBlock method is immensely powerful. It allows us to create a synchronous operation from a block. However, it does not give us the ability to create an asynchronous operation.

If we need an asynchronous operation for a small task, it might be tempting to subclass NSOperation. Usually though, subclassing NSOperation is overkill.

I cooked up a class called AsyncBlockOperation. It's very similar to NSBlockOperation with one caveat; the block you pass to it receives one argument, a reference to the AsyncBlockOperation for the block. When your block is done with its task, it must call [op complete]

Here's an example of its use:

NSOperation* asyncOp = [AsyncBlockOperation blockOperationWithBlock:^(AsyncBlockOperation* op) {
    NSLog(@"starting op");
    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        if (op.isCancelled){
            return [op complete];
        }
        
        NSLog(@"2 seconds have passed");
        NSLog(@"calling [op complete]");
        [op complete];
    });
    NSLog(@"op block returns but op isn't finished");
}];
asyncOp.completionBlock = ^{
    NSLog(@"completion block called!");
};
[asyncOp start];

The output will be:

2013-04-29 22:51:57.417 Experiments[5795:11603] starting op

2013-04-29 22:51:57.419 Experiments[5795:11603] op block returns but op isn't finished

2013-04-29 22:51:59.420 Experiments[5795:11603] 2 seconds have passed

2013-04-29 22:51:59.421 Experiments[5795:11603] calling [op complete]

2013-04-29 22:51:59.422 Experiments[5795:12e03] completion block called!

asyncblockoperation's People

Contributors

churchofthought avatar neonichu avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.