CocoaPods trunk is moving to be read-only. Read more on the blog, there are 14 months to go.
| TestsTested | ✓ |
| LangLanguage | SwiftSwift |
| License | MIT |
| ReleasedLast Release | Feb 2016 |
| SPMSupports SPM | ✗ |
Maintained by ADlai Holler.
Atomic is a fast, safe class for making values thread-safe in Swift. It is backed by pthread_mutex_lock which is the fastest, most-efficient locking mechanism available.
pod Atomic to your Podfilegithub "Adlai-Holler/Atomic" to your Cartfile./// This class is completely thread-safe (yay!).
final class MyCache<Value> {
private let entries: Atomic<[String: Value]> = Atomic([:])
func valueForKey(key: String) -> Value? {
return entries.withValue { $0[key] }
}
func setValue(value: Value, forKey: Key) {
entries.modify { (var dict) in
dict[key] = value
return dict
}
}
func clear() {
entries.value = [:]
}
func copy() -> [String: Value] {
return entries.value
}
}/// Thread-safe manager for the `networkActivityIndicator` on iOS.
final class NetworkActivityIndicatorManager {
static let shared = NetworkActivityIndicatorManager()
private let count = Atomic(0)
func incrementActivityCount() {
let oldValue = count.modify { $0 + 1 }
if oldValue == 0 {
updateUI(true)
}
}
func decrementActivityCount() {
let oldValue = count.modify { $0 - 1 }
if oldValue == 1 {
updateUI(false)
}
}
private func updateUI(on: Bool) {
dispatch_async(dispatch_get_main_queue()) {
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
}
}pthread_mutex_lock is faster than NSLock and more efficient than OSSpinLock.throw errors inside its methods, uses @noescape and generics to make your code as clean as possible.The original version of Atomic.swift was written by the ReactiveCocoa contributors.