Taking Notes

cause i'll forget

Sync Followup

I thought I’d follow my post about concurrency with a simple example that actually uses it:

ESMutableDictionary

This isn’t an NSMutableDictionary subclass as you might expect, it’s a very simple wrapper around a CFMutableDictionary which presents only the most basic interface to manipulating membership in the collection and retrieving current members of the collection.

@interface ESMutableDictionary : NSObject

- (id)init;
- (void)setObject:(id)obj forKey:(id)key;
- (void)removeObjectForKey:(id)key;
- (id)objectForKey:(id)key;

- (NSDictionary *)copyDictionary;

@end

What it is:

  • An easy to use, reasonably performant, key value store that can have values added, removed and retrieved from multiple threads simultaneously.

What it is not:

  • A wholesale replacement for NSMutableDictionary.
    • The concurrency considerations make this collection significantly less performant than a plain dictionary in any situation where there isn’t need for concurrency.
    • Many of the functions that make NSDictionary so useful are not available, because making them concurrent and still being performant would be challenging.
  • Protection against mutating members of the collection. If you toss an object into this collection and then two threads try to changes it’s properties simultaneously, it’s on you to handle that, not this collection.

Copy Dictionary:

This is offered as a way to pop out the current collection so that it can be manipulated or enumerated or etc. It’s a shallow copy, but it should do the trick for most applications.