Git Product home page Git Product logo

atomic's Introduction

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.

atomic's People

Contributors

adlai-holler avatar killobatt avatar readmecritic avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.