PickMe Apr 5, 2025

Swift Thread Safety: NSLockThreadSafeAccess vs DispatchQueueSafeAccess

Article Summary

Kcrdissanayake from PickMe Engineering breaks down a critical choice every Swift developer faces: how to safely manage shared state across threads without killing performance.

Thread safety in Swift doesn't have to mean choosing between simplicity and scalability. This deep dive compares two property wrapper approaches for concurrent access: NSLock-based synchronization versus GCD's barrier pattern. The article includes flowcharts, performance comparisons, and real implementation code.

Key Takeaways

Critical Insight

Choose NSLock for precision and control, DispatchQueue for scalability in read-heavy scenarios, each solving different concurrency challenges.

The article reveals a bonus compatibility layer that makes these wrappers work natively with SwiftUI's objectWillChange publisher.

About This Article

Problem

Swift developers need to pick the right way to protect shared state across threads. The choice comes down to whether you want exclusive locking or allow concurrent reads, and each approach has tradeoffs.

Solution

Kcrdissanayake offers two options. NSLockThreadSafeAccess uses NSLock to handle access one at a time. DispatchQueueSafeAccess uses GCD barriers to let multiple threads read at once. Both work, but they differ in how complex they are to implement.

Impact

If your app does a lot of reading, DispatchQueue's barrier pattern won't slow down readers waiting for each other. NSLock is simpler and works well when reads and writes happen at similar rates. Pick whichever fits your actual usage pattern.