Observing NSMutableDictionary changes
Asked Answered
B

3

19

Is it possible to observe (subscribe to) changes to values stored under different keys in an NSMutableDictionary? In my case the keys would already exist when the subscription is initiated, but the values change and I would like to be notified in this case. I would like the key of the changed value in the notification.

I assume that if my dictionary keys were all NSString instances I could just subscribe to each key path individually. But what if my keys are non-strings? Am I out of luck in that case?

Bambara answered 9/7, 2009 at 22:54 Comment(0)
M
7

That's an interesting idea. I can't find anything in NSDictionary or NSDictionaryController that looks promising. My first instinct would be to use composition around an NSMutableDictionary and intercept calls to setObject:forKey: (and maybe -removeObjectForKey:) to notify subscribers of changes.

There's a Cocoa With Love post on subclassing NSMutableDictionary that will likely be useful should you choose to go that route. I also have created my own NSMutableDictionary subclasses, and you're welcome to use the open-source code.

You could design an observer protocol that could specify particular keys that should be monitored. Shouldn't be too much code, but more than I have time to throw down at the moment.

Maundy answered 9/7, 2009 at 23:13 Comment(2)
Thanks. I think composition around an NSMutableDictionary is the way to go. I still can't see how to make that work unless the keys are NSStrings, but I think I can probably work with that.Bambara
@Quinn Taylor: the link to your custom subclass seems dead and I'd need to check this code out. Do you still have it hanging around somewhere?Chadwickchae
B
6

I've successfully implemented this now, using composition around the NSMutableDictionary. I'm surprised how little code it took. The class I implemented is Board, used to represent the model in a board game. Anyone can subscribe to changes for board states by calling the addObserver: method, implemented like this:

- (void)addObserver:(id)observer {
    for (id key in grid)
        [self addObserver:observer
               forKeyPath:[key description]
                  options:0
                  context:key];
}

Since you can only subscribe to keys using the KVO model, I cheated and subscribed to the description of the key, but passing the key itself as the context. In objects that observe instances of my Board I implement the -observeValueForKeyPath:ofObject:change:context: to ignore the keyPath string and just use the passed-in context.

My simple Board class is not KVO compliant for the artificial properties I create using this method, so I passed 0 in the options property so the KVO machinery will not try to get the old/new values of those keys. That would cause my code to blow up.

Anything that changes the board (in my simple class there is only one method that does this) raise the necessary notifications to cause the KVO machinery to leap into action:

- (void)setPiece:(id)piece atLocation:(Location*)loc {
    [self willChangeValueForKey:[loc description]];
    [grid setObject:piece forKey:loc];
    [self didChangeValueForKey:[loc description]];
}

Voila! Subscription to an NSMutableDictionary with non-string keys!

Bambara answered 10/7, 2009 at 19:49 Comment(0)
M
0

Altthough I am using NSMutableDictionary for different reasons and in a different way (I am not subclassing it) I found another approach.

I am using NSMutableDictionary, since it nicely serializes and deserializes from/to JSON. To get and set values I am using "wrappers". Those are objects that use the dictionaries as "raw" entities and provide getters and setters to access the values. My getter and setter implementations then simply define the keys (and object types).

There also is a base class which provides the property I am passing those dictionaries to (or get them from).

@protocol PMBase <NSObject>
@property (nonatomic, strong, nullable) NSMutableDictionary<NSString*, id>* entity;
@end

@interface MBase : NSObject<PMBase>
@property (nonatomic, strong, nullable) NSMutableDictionary<NSString*, id>* entity;
@end

inline static NSString* _Nullable mbase_get_string(id<PMBase> _Nonnull const obj,
                                                   NSString* _Nonnull const key,
                                                   NSString* _Nullable const fallback) {
    id const val = [obj.entity objectForKey:key];
    return [val isKindOfClass:NSString.class] ? val : fallback;
}

inline static void mbase_set_string(id<PMBase> _Nonnull const obj,
                                    NSString* _Nonnull const key,
                                    NSString* _Nullable const value) {
    if (value) {
        [obj.entity setObject:value forKey:key];
    } else {
        [obj.entity removeObjectForKey:key];
    }
}

inline static NSNumber* _Nullable mbase_get_number(id<PMBase> _Nonnull const obj,
                                                   NSString* _Nonnull const key,
                                                   NSNumber* _Nullable const fallback) {
    id const val = [obj.entity objectForKey:key];
    return [val isKindOfClass:NSNumber.class] ? val : fallback;
}

inline static void mbase_set_number(id<PMBase> _Nonnull const obj,
                                    NSString* _Nonnull const key,
                                    NSNumber* _Nullable const value) {
    if (value) {
        [obj.entity setObject:value forKey:key];
    } else {
        [obj.entity removeObjectForKey:key];
    }
}

(MBase implementation is no magic, so no code here.)

In the subclasses I simply define additional properties, where I override the getters and setters. Subclasses pretty much implement the accessors and sometimes, they return instances of other MBase subclasses.

@protocol PMDevice <NSObject>
// Not important here
@end

@interface MDevice : MBase <PMDevice>

@property (nonatomic, strong, nonnull) NSString* deviceName;
@property (nonatomic, assign) MDeviceType deviceType;             // phone, tablet... 

@end

@implementation MDevice

- (void)setDeviceName:(NSString*)name {
    mbase_set_string(self, @"name", name);
}

- (NSString*)deviceName {
    return mbase_get_string(self, @"name", NSLocalizedString(@"Unnamed", @"unnamed device placeholder"));
}

- (void)setDeviceType:(MDeviceType)deviceType {
    mbase_set_number(self, @"type", [NSNumber numberWithInt:(int)deviceType]);
}

- (MDeviceType)deviceType {
    return (MDeviceType)mbase_get_number(self, @"type", [NSNumber numberWithInt:MDeviceTypeOther]).intValue;
}

@end    

Now I needed the key, wrappers use to access a specific dictionary value. I simply added the "last used key" as property to my MBase and added that line to the getter and setter inlines:

@protocol PMBase <NSObject>
@property (nonatomic, strong, nullable) NSString* lastUsedKey;
@property (nonatomic, strong, nullable) NSMutableDictionary<NSString*, id>* entity;
@end

@interface MBase : NSObject<PMBase>
@property (nonatomic, strong, nullable) NSString* lastUsedKey;
@property (nonatomic, strong, nullable) NSMutableDictionary<NSString*, id>* entity;
@end

inline static NSString* _Nullable mbase_get_string(id<PMBase> _Nonnull const obj,
                                                   NSString* _Nonnull const key,
                                                   NSString* _Nullable const fallback) {
    obj.lastUsedKey = key;
    id const val = [obj.entity objectForKey:key];
    return [val isKindOfClass:NSString.class] ? val : fallback;
}

inline static void mbase_set_string(id<PMBase> _Nonnull const obj,
                                    NSString* _Nonnull const key,
                                    NSString* _Nullable const value) {
    obj.lastUsedKey = key;
    if (value) {
        [obj.entity setObject:value forKey:key];
    } else {
        [obj.entity removeObjectForKey:key];
    }
}

inline static NSNumber* _Nullable mbase_get_number(id<PMBase> _Nonnull const obj,
                                                   NSString* _Nonnull const key,
                                                   NSNumber* _Nullable const fallback) {
    obj.lastUsedKey = key;
    id const val = [obj.entity objectForKey:key];
    return [val isKindOfClass:NSNumber.class] ? val : fallback;
}

inline static void mbase_set_number(id<PMBase> _Nonnull const obj,
                                    NSString* _Nonnull const key,
                                    NSNumber* _Nullable const value) {
    obj.lastUsedKey = key;
    if (value) {
        [obj.entity setObject:value forKey:key];
    } else {
        [obj.entity removeObjectForKey:key];
    }
}

Now, whenever I need information about a key (keys are subject to change if I come across a reason to do so), I am going to access a value getter and then the lastUsedKey property.

Myocardium answered 4/6, 2020 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.