space-code/atomic
## Description
Description
Atomic is a lightweight Swift property wrapper that provides thread-safe access to values. It ensures safe concurrent access to properties without the complexity of manual lock management.
Features
π Thread-Safe - Automatic synchronization for concurrent access β‘ Simple API - Just add @Atomic to any property π― Type-Safe - Works with any Swift type π± Cross-Platform - Works on iOS, macOS, tvOS, watchOS, and visionOS β‘ Lightweight - Minimal footprint with zero dependencies π§ͺ Well Tested - Comprehensive test coverage
Table of Contents
Requirements
| Platform | Minimum Version | |-----------|----------------| | iOS | 13.0+ | | macOS | 10.15+ | | tvOS | 13.0+ | | watchOS | 6.0+ | | visionOS | 1.0+ | | Xcode | 15.3+ | | Swift | 5.10+ |
Installation
Swift Package Manager
Add the following dependency to your Package.swift:
dependencies: [
.package(url: "https://github.com/space-code/atomic.git", from: "1.1.1")
]Or add it through Xcode:
- File > Add Package Dependencies
- Enter package URL:
https://github.com/space-code/atomic.git - Select version requirements
Quick Start
import Atomic
@Atomic var counter = 0
// Thread-safe increment from multiple threads
DispatchQueue.concurrentPerform(iterations: 1000) { _ in
counter += 1
}
print("Final count: \(counter)") // Always 1000Usage
Basic Usage
Simply add the @Atomic property wrapper to any property that needs thread-safe access:
import Atomic
class UserSession {
@Atomic var user: User?
@Atomic var loginAttempts = 0
func login(username: String, password: String) async throws {
// Thread-safe write
_loginAttempts.write { $0 += 1 }
// Perform authentication
let authenticatedUser = try await authenticate(username, password)
// Thread-safe write with new value
_user.write(authenticatedUser)
}
func getCurrentUsername() -> String? {
// Thread-safe read
_user.read { $0?.username }
}
}Thread-Safe Collections
import Atomic
class DataCache {
@Atomic private var cache: [String: Data] = [:]
func store(_ data: Data, forKey key: String) {
_cache.write { cache in
cache[key] = data
}
}
func retrieve(forKey key: String) -> Data? {
_cache.read { $0[key] }
}
func removeExpired(before date: Date) {
_cache.write { cache in
cache = cache.filter { $0.value.timestamp > date }
}
}
}Communication
- π Found a bug? Open an issue
- π‘ Have a feature request? Open an issue
- β Questions? Start a discussion
- π Security issue? Email nv3212@gmail.com
Contributing
We love contributions! Please feel free to help out with this project. If you see something that could be made better or want a new feature, open up an issue or send a Pull Request.
Development Setup
Bootstrap the development environment:
mise installLicense
Atomic is released under the MIT license. See LICENSE for details.
<div align="center">
Made with β€οΈ by space-code
</div>
Package Metadata
Repository: space-code/atomic
Default branch: main
README: README.md