Skip to content

Commit

Permalink
dispatch to main queue on set
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 11, 2024
1 parent bde740c commit eef0df0
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Sources/Defaults/UserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ extension UserDefaults {
}

func _set<Value: Defaults.Serializable>(_ key: String, to value: Value) {
if (value as? _DefaultsOptionalProtocol)?._defaults_isNil == true {
removeObject(forKey: key)
return
}
DispatchQueue.main.async { [self] in
if (value as? _DefaultsOptionalProtocol)?._defaults_isNil == true {
removeObject(forKey: key)
return
}

set(Value.toSerializable(value), forKey: key)
set(Value.toSerializable(value), forKey: key)
}
}

public subscript<Value: Defaults.Serializable>(key: Defaults.Key<Value>) -> Value {
Expand Down

0 comments on commit eef0df0

Please sign in to comment.