Git Product home page Git Product logo

jltdelayoperation's Introduction

JLTDelayOperation

The JLTDelayOperation class is a no-op NSOperation which allows dependent operations to delay their start.

An instance of a JLTDelayOperation is created is a delay duration. Once started, the instance will execute for at least that duration. After the delay has expired, the instance will finish. The instance can be cancelled any time up until it has finished.

Examples

Basic Usage

The simplest way to get a delay operation is create an instance.

JLTDelayOperation *delayOperation = [JLTDelayOperation delayOperationWithDelay:5.0];

The simplest way to execute the operation is to start it.

JLTDelayOperation *delayOperation = [JLTDelayOperation delayOperationWithDelay:5.0];
[delayOperation start];

This will perform a no-op operation which will finish after 5 seconds. This is useful as a way to delay the start of other operations.

JLTDelayOperation *delayOperation = [JLTDelayOperation delayOperationWithDelay:5.0];
NSOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
    NSLog("perform operation");
}];

[operation addDependency:delayOperation];

NSOperationQueue *operationQueue = [[OperationQueue alloc] init];
[operationQueue addOperations:@[delayOperation, operation] waitUntilFinished:NO];

Delay the start of multiple operations

Any number of operations can be delayed by a single JLTDelayOperation.

JLTDelayOperation *delayOperation = [JLTDelayOperation delayOperationWithDelay:5.0];
NSOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{ NSLog("1"); }];
NSOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{ NSLog("2"); }];
NSOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{ NSLog("3"); }];

[operation1 addDependency:delayOperation];
[operation2 addDependency:delayOperation];
[operation3 addDependency:delayOperation];

NSOperationQueue *operationQueue = [[OperationQueue alloc] init];
[operationQueue addOperations:@[delayOperation, operation1, operation2, operation3]
            waitUntilFinished:NO];

Easy Delayed Block Operation

A block operation can be created with a delay using the NSBlockOperation category method -blockOperationWithDelay:andBlock:.

NSArray *operations = [NSBlockOperation blockOperationWithDelay:5.0 andBlock:^{
    NSLog("perform block operation");
}];

NSOperationQueue *operationQueue = [[OperationQueue alloc] init];
[operationQueue addOperations:operations waitUntilFinished:NO];

Notice that an array of operations is returned. The array is a pair of operations. The first operation is a JLTDelayOperation instance, the second operation is an NSBlockOperation instance.

Easy Delayed Block Operation in an Operation Queue

An operation queue can add a block operation with a delay using the NSOperationQueue category method -addOperationWithDelay:andBlock:.

NSOperationQueue *operationQueue = [[OperationQueue alloc] init];

JLTDelayOperation *delayOperation = [operationQueue addOperationWithDelay:5.0 andBlock:^{
    NSLog("perform block operation");
}];

Here a JLTDelayOperation instance is returned. This instance can be used to add multiple operations to the operation queue using the category method -addOperationDependentOnOperation:withBlock:.

NSOperationQueue *operationQueue = [[OperationQueue alloc] init];

JLTDelayOperation *delayOperation = [operationQueue addOperationWithDelay:5.0 andBlock:^{
    NSLog("1");
}];

[operationQueue addOperationDependentOnOperation:delayOperation withBlock:^{
    NSLog("2");
};

[operationQueue addOperationDependentOnOperation:delayOperation withBlock:^{
    NSLog("3");
};

jltdelayoperation's People

Contributors

jefferythomas avatar

Stargazers

Never avatar  avatar Pratik avatar bytelee avatar João Prado Maia avatar

Watchers

 avatar

Forkers

tonyarnold

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.