A Swift microframework for very easy atomic values.

Related tags

Concurrency Atomic
Overview

Atomic

Carthage compatible CocoaPods compatible CocoaPods compatible

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 by adding pod Atomic to your Podfile
  • Using Carthage by adding github "Adlai-Holler/Atomic" to your Cartfile.

How to Use

/// 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
    }
}

Another Example

/// 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 contributors.

You might also like...
 GCDTimer - Well tested Grand Central Dispatch (GCD) Timer in Swift
GCDTimer - Well tested Grand Central Dispatch (GCD) Timer in Swift

GCDTimer Well tested Grand Central Dispatch (GCD) Timer in Swift. Checkout the test file. Usage Long running timer import GCDTimer

Schedule timing task in Swift using a fluent API. (A friendly alternative to Timer)
Schedule timing task in Swift using a fluent API. (A friendly alternative to Timer)

Schedule(简体中文) Schedule is a timing tasks scheduler written in Swift. It allows you run timing tasks with elegant and intuitive syntax. Features Elega

Grand Central Dispatch simplified with swift.

GCDKit GCDKit is Grand Central Dispatch simplified with Swift. for Swift 1.2: Use version 1.0.1 for Swift 2.1 / 2.2: Use the master branch Introductio

Queues, timers, and task groups in Swift
Queues, timers, and task groups in Swift

Dispatcher eases the pain of using Grand Central Dispatch by introducing 4 new Swift classes. Dispatcher Queue Group Timer Requirements Swift 2.0+ Ins

A wrapper of Grand Central Dispatch written in Swift

GCD A wrapper of Grand Central Dispatch written in Swift. Examples gcd // submit your code for asynchronous execution on a global queue with high prio

Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch (iOS7+ and OS X 10.9+ compatible)

Async.legacy Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch (GCD) Async rewritten for iOS7 and OS X 10.9 Compatibility

⏳ Collection of Swift 5.5 async/await utility functions.

⏳ FunAsync Collection of Swift 5.5 async/await utility functions. Throw - Result conversion asyncThrowsToAsyncResult asyncResultToAsyncThrows More C

🎭 Swift async/await & Actor-powered effectful state-management framework.
🎭 Swift async/await & Actor-powered effectful state-management framework.

🎭 Actomaton 🧑‍🎤 Actor + 🤖 Automaton = 🎭 Actomaton Actomaton is Swift async/await & Actor-powered effectful state-management framework inspired by

AutoLogout is a swift library for managing user's session on inactivity.
AutoLogout is a swift library for managing user's session on inactivity.

On user inactivity, it will show an alert box to continue session or Logout as shown in screen shot, according to time set.

Comments
  • Correct the spelling of CocoaPods in README

    Correct the spelling of CocoaPods in README

    This pull requests corrects the spelling of CocoaPods 🤓 https://github.com/CocoaPods/shared_resources/tree/master/media

    opened by ReadmeCritic 2
Owner
Adlai Holler
Adlai Holler
GroupWork is an easy to use Swift framework that helps you orchestrate your concurrent, asynchronous functions in a clean and organized way

GroupWork is an easy to use Swift framework that helps you orchestrate your concurrent, asynchronous functions in a clean and organized way. This help

Quan Vo 42 Oct 5, 2022
A complete set of primitives for concurrency and reactive programming on Swift

A complete set of primitives for concurrency and reactive programming on Swift 1.4.0 is the latest and greatest, but only for Swift 4.2 and 5.0 use 1.

AsyncNinja 156 Aug 31, 2022
Futures is a cross-platform framework for simplifying asynchronous programming, written in Swift.

Futures Futures is a cross-platform framework for simplifying asynchronous programming, written in Swift. It's lightweight, fast, and easy to understa

David Ask 60 Aug 11, 2022
Hydra ⚡️ Lightweight full-featured Promises, Async & Await Library in Swift

Lightweight full-featured Promises, Async & Await Library in Swift What's this? Hydra is full-featured lightweight library which allows you to write b

Daniele Margutti 2k Dec 24, 2022
Kommander is a Swift library to manage the task execution in different threads.

A lightweight, pure-Swift library for manage the task execution in different threads. Through the definition a simple but powerful concept, Kommand.

Intelygenz 173 Apr 11, 2022
SwiftCoroutine - Swift coroutines for iOS, macOS and Linux.

Many languages, such as Kotlin, Go, JavaScript, Python, Rust, C#, C++ and others, already have coroutines support that makes the async/await pattern i

Alex Belozierov 808 Dec 1, 2022
Venice - Coroutines, structured concurrency and CSP for Swift on macOS and Linux.

Venice provides structured concurrency and CSP for Swift. Features Coroutines Coroutine cancelation Coroutine groups Channels Receive-only chan

Zewo 1.5k Dec 22, 2022
Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch

Async Now more than syntactic sugar for asynchronous dispatches in Grand Central Dispatch (GCD) in Swift Async sugar looks like this: Async.userInitia

Tobias Due Munk 4.6k Dec 27, 2022
AwaitKit is a powerful Swift library which provides a powerful way to write asynchronous code in a sequential manner.

AwaitKit is a powerful Swift library inspired by the Async/Await specification in ES8 (ECMAScript 2017) which provides a powerful way to write asynchronous code in a sequential manner.

Yannick Loriot 752 Dec 5, 2022
Elegant ⏱ interface for Swift apps

Each Elegant ⏱ interface for Swift apps Each is a NSTimer bridge library written in Swift. Features Requirements Installation Usage Leaks License Feat

Luca D'Alberti 764 Dec 29, 2022