Skip to content

Commit

Permalink
NSUserDefaults: Only emit change notifications if value changed
Browse files Browse the repository at this point in the history
  • Loading branch information
hmelder committed Oct 25, 2024
1 parent 2223972 commit d3d1529
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions Source/NSUserDefaults.m
Original file line number Diff line number Diff line change
Expand Up @@ -1519,12 +1519,17 @@ - (void) removeObjectForKey: (NSString*)defaultName
if (nil != pd)
{
if ([pd setObject: nil forKey: defaultName])
{
id new;
[self _changePersistentDomain: processName];
new = [self objectForKey: defaultName];
// Emit only a KVO notification when the value has actually changed,
// meaning -objectForKey: would return a different value than before.
if ([new hash] != [old hash])
{
id new;
[self _changePersistentDomain: processName];
new = [self objectForKey: defaultName];
[self _notifyObserversOfChangeForKey: defaultName oldValue:old newValue: new];
[self _notifyObserversOfChangeForKey: defaultName oldValue:old newValue: new];
}
}
else {
// We always notify observers of a change, even if the value
// itself is unchanged.
Expand Down Expand Up @@ -1672,7 +1677,12 @@ - (void) setObject: (id)value forKey: (NSString*)defaultName
// superseded by GSPrimary or NSArgumentDomain
new = [self objectForKey: defaultName];
[self _changePersistentDomain: processName];
[self _notifyObserversOfChangeForKey: defaultName oldValue:old newValue:new];

// Emit only a KVO notification when the value has actually changed
if ([new hash] != [old hash])
{
[self _notifyObserversOfChangeForKey: defaultName oldValue:old newValue:new];
}
}
else
{
Expand Down

0 comments on commit d3d1529

Please sign in to comment.