Latest 1.0.1
Homepage https://github.com/domhof/DHThreadSafeWrapper
License MIT
Platforms ios 9.0, osx 10.12, tvos 10.0, watchos 3.0
Authors

Combines a value with a dispatch queue. The dispatch queue is used to handle concurrent read/write access.

Examples

// A boolean value only accessed on an internal dispatch queue.
let someVar = ThreadSafe(false)

// Synchronously read
print("someVar: (someVar.value)")

// Synchronously write
someVar.syncWrite { $0 = true } 

someVar.asyncRead { (value) in
    // Do some work that should be done together with reading the value...
}

someVar.asyncWrite { (value) in
    // Do some work that should be done together with writing the value...
    value = false
}

// Update the value after one second.
let workItem = someVar.writeAfter(after: 1.0) { (value) in
    // Do some work that should be done together with writing the value...
    value = false
}
// ...something has changed and we don't want to update it anymore.
workItem.cancel()

// Using one queue to synchronize read/write access over multiple instances.
let queue = DispatchQueue(label: "com.dominikhofer.myQueue", qos: .background, attributes: .concurrent)
let usingTheSameQueue1 = ThreadSafe(0, dispatchQueue: queue)
let usingTheSameQueue2 = ThreadSafe(0, dispatchQueue: queue)
usingTheSameQueue1.asyncWrite { $0 += 1 }
usingTheSameQueue2.asyncWrite { $0 += 1 }
// ...

Latest podspec

{
    "name": "DHThreadSafeWrapper",
    "version": "1.0.1",
    "summary": "Combines a value with a dispatch queue. The dispatch queue is used handle concurrent read/write access.",
    "homepage": "https://github.com/domhof/DHThreadSafeWrapper",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Dominik Hofer": "[email protected]"
    },
    "social_media_url": "https://twitter.com/dominikhofer",
    "source": {
        "git": "https://github.com/domhof/DHThreadSafeWrapper.git",
        "tag": "1.0.1"
    },
    "source_files": "DHThreadSafeWrapper/**/*.swift",
    "platforms": {
        "ios": "9.0",
        "osx": "10.12",
        "tvos": "10.0",
        "watchos": "3.0"
    },
    "swift_version": "4.2"
}

Pin It on Pinterest

Share This