TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Aug 2015 |
Maintained by Héctor Marqués.
NSOperation
subclass that manages the concurrent execution of a block.
You can use MROperation
objects directly:
MROperation *someOperation = [[MROperation alloc] initWithBlock:^(id<MRExecutingOperation> *operation) {
// long ruinning task...
if (operation.isCancelled) return;
// long ruinning 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.
Drag the MROperation folder into your project.
MROperation is available under the MIT license. See the LICENSE file for more info.