Skip to content

Instantly share code, notes, and snippets.

@kwylez
Created September 5, 2013 15:05
Show Gist options
  • Save kwylez/6451427 to your computer and use it in GitHub Desktop.
Save kwylez/6451427 to your computer and use it in GitHub Desktop.
Thread safe Antenna approach
- (void)addChannel:(id <AntennaChannel>)channel forName:(NSString *)name {
dispatch_barrier_async(_channelsThreadQueue, ^{
/**
* Has this channel already been added?
*/
if ([self channelExists:name]) {
return;
}
NSDictionary *notifInfo = @{AntennaChannelNotificationDictKey : name};
self.channels[name] = channel;
[[NSNotificationCenter defaultCenter] postNotificationName:AntennaChannelAddedNotification
object:nil
userInfo:notifInfo];
});
}
- (void)removeChannelForName:(NSString *)name {
dispatch_barrier_async(_channelsThreadQueue, ^{
/**
* Has this channel already been removed?
*/
if (![self channelExists:name]) {
return;
}
[self.channels removeObjectForKey:name];
NSDictionary *notifInfo = @{AntennaChannelNotificationDictKey : name};
[[NSNotificationCenter defaultCenter] postNotificationName:AntennaChannelRemovedNotification
object:nil
userInfo:notifInfo];
});
}
- (id <AntennaChannel>)channelForName:(NSString *)name {
id <AntennaChannel> channelObject = nil;
dispatch_sync(_channelsThreadQueue, ^{
channelObject = self.channels[name];
});
return channelObject;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment