Thread Safe Property and Resource Access with the Transaction Wrapper
Here is a transaction type to copy & paste into projects to encapsulates thread-safe read/write access: Just make sure to not use the main queue, because .sync
call from main to main will deadlock your app! It ensures you read values synchronously, which isn’t dangerous, and enqueue and execute write operations in order. This is useful if you need to access any resource from multiple threads and want to avoid the overhead of mutex locks.
UserDefaults Access via Property Wrappers Is Worse Than You Might Think
I really want to like Swift property wrappers for UserDefaults
. But I have a hard time making them 100% useful. Take the post by Andy Ibanez on the topic for example. It’s a well-written post with great code examples. If you want copy-able code for your app, read it. It’s good.