Latest 0.1.0
Homepage https://github.com/hectr/MROperation
License MIT
Platforms ios 5.0, osx 10.7, requires ARC
Authors

CI Status
Version
License
Platform

NSOperation subclass that manages the concurrent execution of a block.

Usage

You can use MROperation objects directly:

MROperation *someOperation = [[MROperation  alloc] initWithBlock:^(id<MRExecutingOperation> *operation) {
    // long running task...
    if (operation.isCancelled) return;
    // long running task...
    if (!operation.isFinished) [operation finishWithError:nil];
};

[someOperation start];

But you can also implement your own subclasses. See, for instance, how you could create a custom subclass for performing reverse-geocoding requests:

// MROperation subclass that performs reverse-geocoding requests.
@interface GeocodingRequestOperation : MROperation

// Returns a reverse-geocoding request operation for the given location.
+ (instancetype)operationWithLocation:(CLLocation *)location;

// The result of the reverse-geocoding request.
@property (nonatomic, strong) CLPlacemark *placemark;

@end

@implementation GeocodingRequestOperation

+ (instancetype)operationWithLocation:(CLLocation *)location {
    return [[self alloc] initWithBlock:^(GeocodingRequestOperation<MRExecutingOperation> *operation) {
        [[[CLGeocoder alloc] init] reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
            operation.placemark = placemarks.firstObject;
            if (!operation.isFinished) [operation finishWithError:error];
        }];
    }];
}

@end

And here’s how you would execute them:

// Set up a queue:
_geocodingQueue = [[NSOperationQueue alloc] init];
_geocodingQueue.maxConcurrentOperationCount = 1;

// Create and configure the reverse-geocoding request operation:
GeocodingRequestOperation *o = [GeocodingRequestOperation operationWithLocation:location];
[o setCompletionBlockWithSuccess:^(GeocodingRequestOperation *operation) {
    NSLog(@"Hello %@!", operation.placemark.country);
} failure:^{
    NSLog(@"%@", error);
}];

// Add the operation to the queue:
[_geocodingQueue addOperation:o];

For another example on how to subclass MRoperation, you can refer to the MRDetectBpmOperation implementation.

Installation

CocoaPods

MROperation is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "MROperation"

Manually

Drag the MROperation folder into your project.

License

MROperation is available under the MIT license. See the LICENSE file for more info.

Alternatives

Latest podspec

{
    "name": "MROperation",
    "version": "0.1.0",
    "summary": "NSOperation subclass that manages the concurrent execution of a block.",
    "description": "The `MROperation` class is a concrete subclass of `NSOperation` that manages the concurrent execution of a block.",
    "homepage": "https://github.com/hectr/MROperation",
    "license": "MIT",
    "authors": {
        "hectr": "[email protected]"
    },
    "source": {
        "git": "https://github.com/hectr/MROperation.git",
        "tag": "0.1.0"
    },
    "social_media_url": "https://twitter.com/hectormarquesra",
    "platforms": {
        "ios": "5.0",
        "osx": "10.7"
    },
    "requires_arc": true,
    "source_files": "MROperation"
}

Pin It on Pinterest

Share This