Created
September 5, 2013 15:05
-
-
Save kwylez/6451427 to your computer and use it in GitHub Desktop.
Thread safe Antenna approach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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