GithubHelp home page GithubHelp logo

timvansteenis / swift-cancellationtoken Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tomlokhorst/swift-cancellationtoken

0.0 1.0 0.0 48 KB

CancellationToken in Swift, modelled after the .NET version

License: MIT License

Ruby 11.88% Swift 88.12%

swift-cancellationtoken's Introduction

swift-cancellationtoken


A "cancellation token" is a little object that can be passed around to one or more asychronous tasks. This token can be used to indicate a user is no longer interested in the result of an asynchronous task.

By using a CancellationToken, the creation and cancellation of asynchronous tasks is separated into two distinct parts.

Usage

  1. Create a CancellationTokenSource.
  2. Get the token from the source.
  3. Pass the token to some asynchronous task.
  4. The asynchronous task may pass the token along to its subtasks.
  5. When no longer interested in the result of the asynchronous task; Cancel the source. This cancellation will propagate to the token, and all copies of the token.
  6. The asynchrounous task may choose to cancel its operation and fail in its own way.

Example

Here's an example based on the binding to Alamofire:

// Create a CancellationTokenSource and a CancellationToken
let source = CancellationTokenSource()
let token = source.token

// Start the asynchroneous downloading of a large file, passing in the cancellation token
request(largeFileUrl, cancellationToken: token)
  .response { response in
    if let error = response.error as? NSError {
      if error.domain == NSURLErrorDomain && error.code == NSURLErrorCancelled {
        print("The downloading was cancelled")
      }
    }
    else {
      print("The response is available: \(response)")
    }
  }

// Some time later, request cancellation
source.cancel()

Installation

CocoaPods

CancellationToken is available for both iOS and OS X. Using CocoaPods, CancellationToken can be integrated into your Xcode project by specifying it in your Podfile:

pod 'CancellationToken'

Then, run the following command:

$ pod install

Manual

CancellationToken is just a single file, so instead of using CocoaPods, you could also just copy it into your project:

Releases

  • 1.0.0 - 2017-02-23 - Cancel on denit of cancellation source
  • 0.3.0 - 2016-09-20 - Swift 3 support
  • 0.2.0 - 2015-09-11 - Swift 2 support
  • 0.1.1 - 2015-06-24 - No more circular references, fixes memory leak
  • 0.1.0 - 2015-03-31 - Initial public release
  • 0.0.0 - 2014-10-31 - Initial private version for project at Q42

Licence & Credits

CancellationToken is written by Tom Lokhorst and available under the MIT license, so feel free to use it in commercial and non-commercial projects. This library modelled after the .NET cancellation model.

swift-cancellationtoken's People

Contributors

tomlokhorst 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.