Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Adlai-Holler/Atomic
A Swift microframework for very easy atomic values.
https://github.com/Adlai-Holler/Atomic
Last synced: 9 days ago
JSON representation
A Swift microframework for very easy atomic values.
- Host: GitHub
- URL: https://github.com/Adlai-Holler/Atomic
- Owner: Adlai-Holler
- License: mit
- Created: 2015-12-05T19:12:42.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2019-05-21T03:12:47.000Z (over 5 years ago)
- Last Synced: 2024-10-14T09:21:11.084Z (26 days ago)
- Language: Swift
- Size: 16.6 KB
- Stars: 35
- Watchers: 3
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-swift-cn - Atomic - tested wrapper for making values thread-safe. (Libs / Thread)
README
## Atomic
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) [![CocoaPods compatible](https://img.shields.io/cocoapods/v/Atomic.svg)](https://cocoapods.org) [![CocoaPods compatible](https://img.shields.io/cocoapods/p/Atomic.svg)](https://cocoapods.org)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.
### Installation
- Using [CocoaPods](https://cocoapods.org) by adding `pod Atomic` to your Podfile
- Using [Carthage](https://github.com/Carthage/Carthage) by adding `github "Adlai-Holler/Atomic"` to your Cartfile.### How to Use
```swift
/// This class is completely thread-safe (yay!).
final class MyCache {
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
}
}
```#### Another Example
```swift
/// 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
}
}
}
```### Features
- Safe. No need to remember to unlock.
- Fast. `pthread_mutex_lock` is faster than `NSLock` and more efficient than `OSSpinLock`.
- Modern. You can safely `throw` errors inside its methods, uses `@noescape` and generics to make your code as clean as possible.
- Tested. This thing is tested like crazy, including accessing it concurrently from 100,000 operations!### Attribution
The original version of `Atomic.swift` was written by the [ReactiveCocoa](https://github.com/ReactiveCocoa/ReactiveCocoa) contributors.