CoreOperation 1.1.0

CoreOperation 1.1.0

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Mar 2016

Maintained by Gabriel Massana.



  • By
  • Gabriel Massana

CoreOperation-iOS

Wrapper project to simplify NSOperation and NSOperationQueue.

Installation

Podfile

platform :ios, '8.0'
pod 'CoreOperation', '~> 1.0'

Then, run the following command:

$ pod install

Old school

Drag into your project the folder /CoreOperation-iOS. That's all.

Example

Register Operation Queues

//  AppDelegate.m

NSString *const kCOMLocalOperationQueueTypeIdentifier = @"kCOMLocalOperationQueueTypeIdentifier";


#pragma mark - OperationQueues

- (void)registerOperationQueues
{
    //Local Background
    NSOperationQueue *localOperationQueue = [[NSOperationQueue alloc] init];
    localOperationQueue.qualityOfService = NSQualityOfServiceBackground;
    localOperationQueue.maxConcurrentOperationCount = 1;

    [[COMOperationQueueManager sharedInstance] registerOperationQueue:localOperationQueue
                                             operationQueueIdentifier:kCOMLocalOperationQueueTypeIdentifier];
}

Create an COMOperation subclass

//  COMExampleOperation.h

@interface COMExampleOperation : COMOperation

- (instancetype)initWithValue:(NSInteger)value;

@end

//  COMExampleOperation.m

@interface COMExampleOperation ()

@property (nonatomic, assign) NSInteger value;

@end

@implementation COMExampleOperation

@synthesize identifier = _identifier;

#pragma mark - Init

- (instancetype)initWithValue:(NSInteger)value
{
    self = [super init];

    if (self)
    {
        self.value = value;
    }

    return self;
}

#pragma mark - Identifier

- (NSString *)identifier
{
    if (!_identifier)
    {
        _identifier = [NSString stringWithFormat:@"COMExampleOperation-%@", @(self.value)];
    }

    return _identifier;
}

#pragma mark - Start

- (void)start
{
    [super start];

    srand((unsigned)time(0));
    NSInteger mod = self.value;
    NSInteger random = rand();
    NSInteger result = random % mod;

    [self didSucceedWithResult:@(result)];
}

#pragma mark - Cancel

- (void)cancel
{
    [super cancel];

    [self didSucceedWithResult:nil];
}

Use the operaation

- (void)operationExample
{
    COMExampleOperation *operation = [[COMExampleOperation alloc] initWithValue:15];

    operation.operationQueueIdentifier = kCOMLocalOperationQueueTypeIdentifier;

    operation.onSuccess = ^(id result)
    {
        NSLog(@"%@", result);
    };

    [[COMOperationQueueManager sharedInstance] addOperation:operation];
}

License

ButtonBackgroundColor-iOS is released under the MIT license. Please see the file called LICENSE.

Versions

$ git tag -a 1.1.0 -m 'Version 1.1.0'

$ git push --tags

Author

Gabriel Massana

Found an issue?

Please open a new Issue here if you run into a problem specific to CoreOperation-iOS, have a feature request, or want to share a comment.