Giter Site home page Giter Site logo

rgcanceltoken's Introduction

RGCancelToken

Simple implementation of cancel tokens normally used for canceling asynchronous operations

The inspiration of this utility class is from the Cancellation Tokens implementation in .NET which can be found here.

https://msdn.microsoft.com/en-us/library/dd997289%28v=vs.110%29.aspx

Copying the description from the above link:

A CancellationToken enables cooperative cancellation between threads, thread pool work items, or Task objects. An object creates a cancellation token by using a CancellationTokenSource, and then passes the cancellation token to any number of threads or objects that should receive notice of cancellation. The token cannot be used to initiate cancellation. When the owning object calls Cancel on the CancellationTokenSource, the IsCancellationRequested property on every copy of the cancellation token is set to true. The objects that receive the notification can respond in whatever manner is appropriate.

Instantiation:

    //creates an instance of RGCancellationTokenSource and assign it to a property so we have a pointer to it.
    RGCancellationTokenSource *cancelTokenSource = [RGCancellationTokenSource new];
    self.cancelTokenSource = cancelTokenSource;

Canceling the task:

    [self.cancelTokenSource cancel];

Reacting to cancellation request

    [self.activityIndicatorView startAnimating];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        cancelToken.cancellationRequestedBlock = ^() {
            
            //this block gets called when RGCancellationTokenSource's cancel method is called
            //react to a cancellation request here
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.activityIndicatorView stopAnimating];
            });
        };
        
        //do your background code here...
        
        while (true) {
            //you can also check if there is a request to cancel using the isCanceled property.
            //below code only continues if there is still no request for cancellation.
            if (cancelToken.isCanceled) {
                break;
            }
            else {
                //continue if not yet canceled.
                //just sleep for a second for demo purposes
                sleep(1);
            }
        }
    });

rgcanceltoken's People

Contributors

ryangoce avatar ryansaperium avatar

Watchers

 avatar

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.