Hey!
Thanks for the detailed answer and sorry for the delayed response. Wasn't aware that why questions usually aren't answered and will keep that in mind next time!
If subclassing is the primary concern, does that mean that nonisolated(unsafe) is actually pretty safe to use as long as I don't use a subclass of UserDefaults?
I am a bit hesitant to create a new instance every time I access user defaults, as I am not sure if that has any performance impacts. Probably they would be very minor anyways, but still I am concerned.
Post
Replies
Boosts
Views
Activity
Hey, thanks for your answer.
While I get the two points, I think UserDefaults does not have user info dictonaries. I get that custom subclasses can be problematic, but up to my knowledge, if an open class would be marked Sendable and someone tried to subclass it, swift would emit a warning that the subclass must also be Sendable. Also, certain other classes in Foundation (like NumberFormatter) are Sendable, despite being open.
In my particular case I cannot use the static UserDefaults.standard as I need to share the defaults within my app group.
Potential code snippet could be:
public final class Defaults: Sendable {
private let defaults: UserDefaults
public init(
_ defaults: UserDefaults = UserDefaults(suiteName: "foobar")
?? .standard
) {
self.defaults = defaults
}
I know I could in theory just pass the suite name and make UserDefaults computed (btw, is UserDefaults.standard also computed?), however I still don't get why UserDefaults is not sendable despite the docs mentioning its thread safe.
I've also filed a radar: FB10224177