dispatch to main queue on set

This commit is contained in:
Sindre Sorhus 2024-02-11 16:42:49 +07:00
parent bde740c9b4
commit eef0df0930
1 changed files with 7 additions and 5 deletions

View File

@ -10,6 +10,7 @@ extension UserDefaults {
} }
func _set<Value: Defaults.Serializable>(_ key: String, to value: Value) { func _set<Value: Defaults.Serializable>(_ key: String, to value: Value) {
DispatchQueue.main.async { [self] in
if (value as? _DefaultsOptionalProtocol)?._defaults_isNil == true { if (value as? _DefaultsOptionalProtocol)?._defaults_isNil == true {
removeObject(forKey: key) removeObject(forKey: key)
return return
@ -17,6 +18,7 @@ extension UserDefaults {
set(Value.toSerializable(value), forKey: key) set(Value.toSerializable(value), forKey: key)
} }
}
public subscript<Value: Defaults.Serializable>(key: Defaults.Key<Value>) -> Value { public subscript<Value: Defaults.Serializable>(key: Defaults.Key<Value>) -> Value {
get { _get(key.name) ?? key.defaultValue } get { _get(key.name) ?? key.defaultValue }