Skip to content

Simple NSOperation subclass that allows a block of asynchronous work to be executed in parallel in a NSOperationQueue

License

Notifications You must be signed in to change notification settings

nobre84/RNConcurrentBlockOperation

Repository files navigation

RNConcurrentBlockOperation

RNConcurrentBlockOperation is a simple NSOperation subclass, similar to NSBlockOperation. It allows a block of work that is potentially asynchrounous to be submited into a NSOperationQueue for parallel execution.

Sample usage:

    NSOperationQueue *queue = [NSOperationQueue new];
    queue.maxConcurrentOperationCount = 5;
    // Regular usage, only finishes the operation
    [queue addOperation:[RNConcurrentBlockOperation operationWithBlock:^(RNCompletionBlock completion) {
        NSLog(@"Concurrent op started");
        //Some async operation
        //... ... ...
        //Async operation completed
        completion(nil);
    }]];
    // Cancelled operation, cancels then finish the operation
    [queue addOperation:[RNConcurrentBlockOperation operationWithBlock:^(RNCompletionBlock completion) {
        NSLog(@"Cancellable op started");
        //Some async operation
        //... ... ...
        //Something happened (i.e user cancelled, network outage), and we want to bail
        //Async operation cancelled
        NSError *error = [NSError new]; //Some possible error
        completion(@{RNOperationStatusKey: RNOperationStatusCanceled, RNOperationErrorKey: error});
    }]];
    // Store the operation value in the userInfo dictionary and finishes it.
    [queue addOperation:[RNConcurrentBlockOperation operationWithBlock:^(RNCompletionBlock completion) {
        NSLog(@"Concurrent op started with result");
        NSString *result = @"A string generated by the async operation";
        completion(@{RNOperationResultKey: result});
    }]];  

About

Simple NSOperation subclass that allows a block of asynchronous work to be executed in parallel in a NSOperationQueue

Resources

License

Stars

Watchers

Forks

Packages

No packages published