From eef0df09305106ff18ba9d575c1144d3c886c62d Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 11 Feb 2024 16:42:49 +0700 Subject: [PATCH] dispatch to main queue on set --- Sources/Defaults/UserDefaults.swift | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Sources/Defaults/UserDefaults.swift b/Sources/Defaults/UserDefaults.swift index 5e1bc2f..ac07def 100644 --- a/Sources/Defaults/UserDefaults.swift +++ b/Sources/Defaults/UserDefaults.swift @@ -10,12 +10,14 @@ extension UserDefaults { } func _set(_ 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(key: Defaults.Key) -> Value {