Latest 1.0.1
Homepage https://github.com/duemunk/Thread
License MIT
Platforms osx 10.10, ios 8.0, tvos 9.0, requires ARC
Frameworks Foundation
Authors

Thread [Thread]()
Thread
CocoaPods compatible

A simple wrapper on NSThread to run blocks on exactly the same thread. It’s guaranteed First-In-First-Out (FIFO).

This code is based on the following StackOverflow answer: http://stackoverflow.com/a/22091859. Permission to redistribute has been granted by the original author (Marc Haisenko).

let thread = Thread()
thread.enqueue {
    // Block is run on the thread
}

Blocks are removed from the queue just before they get run.

Start and cancel the life time of a thread:

// Initialize unstarted
let thread = Thread(start: false)
thread.enqueue {
    // ...
}
thread.enqueue {
    // ...
}
// Start the thread to begin running queued up blocks
thread.start()
// and maybe stop the thread again. Blocks still in the queue
thread.cancel()

Pause and resume:

let thread = Thread()
thread.enqueue {
    // ...
}
// Pause
thread.pause()

// ... do other stuff

thread.enqueue {
    //...
}
// Begin running blocks from queue again
thread.resume()

Empty queue:

// Remove any blocks still in queue
thread.emptyQueue()

Pre-enqueue with blocks:

let thread = Thread(queue: [
    { /* … */ },
    { /* … */ }
])

When not to use

Thread is very strict for running on exactly the same thread. For do-on-background-return-on-main stuff, use the brilliant Async wrapper for GCD.

License

The MIT License (MIT)

Copyright © 2015 Tobias Due Munk

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Latest podspec

{
    "name": "Thread",
    "version": "1.0.1",
    "summary": "A simple wrapper on NSThread to run blocks on exactly the same thread. Itu2019s guaranteed First-In-First-Out (FIFO).",
    "homepage": "https://github.com/duemunk/Thread",
    "license": "MIT",
    "authors": {
        "Tobias Due Munk": "[email protected]"
    },
    "platforms": {
        "osx": "10.10",
        "ios": "8.0",
        "tvos": "9.0"
    },
    "source": {
        "git": "https://github.com/duemunk/Thread.git",
        "tag": "1.0.1"
    },
    "source_files": "Source/*.swift",
    "requires_arc": true,
    "frameworks": "Foundation"
}

Pin It on Pinterest

Share This