Skip to content

Instantly share code, notes, and snippets.

@hollance
Last active September 25, 2017 03:35
Show Gist options
  • Save hollance/3121457 to your computer and use it in GitHub Desktop.
Save hollance/3121457 to your computer and use it in GitHub Desktop.
Communicate between objects using channels

Communicate between objects using channels

When you have two objects A and B, say two view controllers, that you want to have talk to each other, you can choose from the following options:

  • NSNotificationCenter. This is anonymous one-to-many communication. Object A posts a notification to the NSNotificationCenter, which then distributes it to any other objects listening for that notification, including Object B. A and B do not have to know anything about each other, so this is a very loose coupling. Maybe a little too loose...

  • KVO (Key-Value Observing). One object observes the properties of another. This is a very tight coupling, because Object B is now peeking directly into Object A. The advantage of KVO is that Object A doesn't have to be aware of this at all, and therefore does not need to send out any notifications -- the KVO mechanism takes care of this behind the scenes.

  • Direct pointers. Object A has a pointer to Object B and directly sends it messages when something of interest happens. This is the tightest coupling possible because A and B cannot function without each other. In the case of view controllers you generally want to avoid this.

  • Delegates. Object B is a delegate of Object A. In this scenario, Object A does not know anything about Object B. It just knows that some object performs the role of its delegate and it will happily send messages to that delegate, but it doesn't know -- or care -- that this is Object B. The delegate pattern is often the preferred way to communicate between view controllers, but it takes some work to set up.

  • Blocks. Essentially the same approach as delegates, except that Object B now gives Object A one or more blocks (closures) to be executed when certain events take place. There is no formal delegate protocol and the only thing that Object A sees of Object B is the blocks it is given.

That is quite a few possibilities, each with its advantages and disadvantages, and the trick is to pick the one that is best suited for the particular communication problem you're trying to solve.

In the end, they are all variations on the observer-notifier pattern. And if none fit, you can always write your own version. :-)

Choose your channels

I was playing with another mechanism that I call "channels" that is a mix of NSNotificationCenter, delegates and blocks. Rather than making Objects A and B communicate directly, they do this through an intermediary, the channel:

+------------------------------------------------------+
|                                                      |
|                        Channel                       |
|                                                      |
+------------------------------------------------------+
		   ^                             ^
		   |  { ... }                    |  { ... }                      
		   |                             |                      
	+------+-------+              +------+-------+              
	|              |              |              |              
	|              |              |              |              
	|   Object A   |              |   Object B   |              
	|              |              |              |              
	|              |              |              |              
	+--------------+              +--------------+              

This is perfectly achievable using NSNotificationCenter (either the global one or a private one for each channel), but I wanted something a bit more lightweight that is simpler to use.

A channel is identified simply by a name, an NSString. If two or more objects use the same channel name, then they are communicating. An object can post a message to the channel and/or listen for messages from other objects.

To listen to a channel you simply do:

[self mh_listenOnChannel:@"MyChannel" block:^(id sender, NSDictionary *dict)
{
	// do something with the dictionary
}];

To post a message to that same channel:

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:...];
[self mh_post:dictionary toChannel:@"MyChannel"];

Or with the new Objective-C literal syntax:

[self mh_post:@{ @"Success" : @YES } toChannel:@"MyChannel"];

The dictionary contains any data that you wish to send along. For example, a view controller that is closing could set a field that indicates which button the user tapped, Cancel or Save.

And that's all you have to do to make these two objects communicate. When an object calls mh_post:toChannel:, any blocks that were registered by listeners for that channel will be executed.

There is no need to unregister from a channel (although you can if you want to). The channel only keeps weak references to its listeners, so if a listener gets deallocated the channel will no longer try to send it notifications.

This functionality is implemented as a category on NSObject.

#include <dispatch/dispatch.h>
typedef void (^MHChannelsBlock)(id sender, NSDictionary *dictionary);
/*!
* A "channel" is like a private NSNotificationCenter between just two objects
* (although more are possible).
*
* Instead of making your objects, such as two view controllers, communicate
* directly with one another through sharing pointers or making one a delegate
* of the other, you can have them communicate through a channel. Objects can
* post messages to the channel and/or listen to messages from other objects.
*
* There is no need to create channels before you use them. A channel is
* identified by a unique name (an NSString). If two objects use the same
* channel name, then they are communicating.
*
* The order in which messages are delivered is arbitrary, so if you have more
* than one listener on the channel you should not assume anything about which
* one is called first. To force a delivery order, give each listener its own
* priority. The default priority is 0. Higher priorities go first.
*
* The listener block is always executed synchronously on the thread that the
* poster runs on, except when queue is not nil. If you pass in a queue, then
* the block is called _asynchronously_ on that queue.
*
* The channel keeps a weak reference to any listeners, so you do not have to
* explicitly remove the listener from the channel before it gets deallocated.
*/
@interface NSObject (MHChannels)
- (void)mh_post:(NSDictionary *)dictionary toChannel:(NSString *)channelName;
- (void)mh_listenOnChannel:(NSString *)channelName block:(MHChannelsBlock)block;
- (void)mh_listenOnChannel:(NSString *)channelName priority:(NSInteger)priority queue:(dispatch_queue_t)queue block:(MHChannelsBlock)block;
- (void)mh_removeFromChannel:(NSString *)channelName;
- (void)mh_debugChannels;
@end
#import "NSObject+MHChannels.h"
@interface MHChannelListener : NSObject
@property (nonatomic, weak) id object;
@property (nonatomic, copy) MHChannelsBlock block;
@property (nonatomic, assign) NSInteger priority;
@property (nonatomic, assign) dispatch_queue_t queue;
@end
@implementation MHChannelListener
@synthesize object;
@synthesize block;
@synthesize priority;
@synthesize queue;
- (NSString *)description
{
return [NSString stringWithFormat:@"%@ object = %@", [super description], object];
}
@end
@implementation NSObject (MHChannels)
- (NSMutableDictionary *)mh_channelsDictionary
{
static dispatch_once_t pred;
static NSMutableDictionary *dictionary;
dispatch_once(&pred, ^{ dictionary = [NSMutableDictionary dictionaryWithCapacity:4]; });
return dictionary;
}
- (void)mh_pruneDeadListenersFromChannel:(NSString *)channelName
{
NSMutableDictionary *channelsDictionary = [self mh_channelsDictionary];
NSMutableArray *listeners = [channelsDictionary objectForKey:channelName];
NSMutableSet *listenersToRemove = nil;
for (MHChannelListener *listener in listeners)
{
if (listener.object == nil)
{
if (listenersToRemove == nil)
listenersToRemove = [NSMutableSet set];
[listenersToRemove addObject:listener];
}
}
if (listenersToRemove != nil)
{
for (MHChannelListener *listener in listenersToRemove)
[listeners removeObject:listener];
if ([listeners count] == 0)
[channelsDictionary removeObjectForKey:channelName];
}
}
- (void)mh_post:(NSDictionary *)dictionary toChannel:(NSString *)channelName
{
NSParameterAssert(channelName != nil);
NSMutableDictionary *channelsDictionary = [self mh_channelsDictionary];
@synchronized (channelsDictionary)
{
NSMutableArray *listeners = [channelsDictionary objectForKey:channelName];
if (listeners != nil)
{
for (MHChannelListener *listener in listeners)
{
if (listener.object != nil)
{
if (listener.queue == nil)
listener.block(listener, dictionary);
else
dispatch_async(listener.queue, ^{ listener.block(listener, dictionary); });
}
}
[self mh_pruneDeadListenersFromChannel:channelName];
}
}
}
- (void)mh_listenOnChannel:(NSString *)channelName block:(MHChannelsBlock)block
{
[self mh_listenOnChannel:channelName priority:0 queue:nil block:block];
}
- (void)mh_listenOnChannel:(NSString *)channelName priority:(NSInteger)priority queue:(dispatch_queue_t)queue block:(MHChannelsBlock)block
{
NSParameterAssert(channelName != nil);
NSParameterAssert(block != nil);
NSMutableDictionary *channelsDictionary = [self mh_channelsDictionary];
@synchronized (channelsDictionary)
{
NSMutableArray *listeners = [channelsDictionary objectForKey:channelName];
if (listeners == nil)
{
listeners = [NSMutableArray arrayWithCapacity:2];
[channelsDictionary setObject:listeners forKey:channelName];
}
MHChannelListener *listener = [[MHChannelListener alloc] init];
listener.object = self;
listener.block = block;
listener.priority = priority;
listener.queue = queue;
[listeners addObject:listener];
[self mh_pruneDeadListenersFromChannel:channelName];
[listeners sortUsingComparator:^(MHChannelListener *obj1, MHChannelListener *obj2)
{
if (obj1.priority < obj2.priority)
return NSOrderedDescending;
else if (obj1.priority > obj2.priority)
return NSOrderedAscending;
else
return NSOrderedSame;
}];
}
}
- (void)mh_removeFromChannel:(NSString *)channelName
{
NSParameterAssert(channelName != nil);
NSMutableDictionary *channelsDictionary = [self mh_channelsDictionary];
@synchronized (channelsDictionary)
{
NSMutableArray *listeners = [channelsDictionary objectForKey:channelName];
if (listeners != nil)
{
for (MHChannelListener *listener in listeners)
{
if (listener.object == self)
listener.object = nil;
}
[self mh_pruneDeadListenersFromChannel:channelName];
}
}
}
- (void)mh_debugChannels
{
NSMutableDictionary *channelsDictionary = [self mh_channelsDictionary];
@synchronized (channelsDictionary)
{
NSLog(@"Channels dictionary: %@", channelsDictionary);
}
}
@end
@havegone
Copy link

havegone commented Sep 9, 2013

i think in mh_post: toChannel: method to fire block should exclude poster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment