A property wrapper to declare atomic properties in swift
pod 'AtomicSwift'
From Xcode, select File → Swift Packages → Add Package Dependency → Select your project → Search AtomicSwift
Just add the @Atomic
qualifier before your variable declaration.
// the following example will crash
var aDictionary = [Int: Int]()
DispatchQueue.concurrentPerform(iterations: 1000) { _ in
self.aDictionary[.random(in: 0...10000)] = "test"
}
// the following example won't crash
@Atomic
var anAtomicDictionary = [Int: Int]()
DispatchQueue.concurrentPerform(iterations: 1000) { _ in
self.anAtomicDictionary[.random(in: 0...10000)] = "test"
}