How to use Reactive Cocoa with notifications

meisel picture meisel · Aug 12, 2013 · Viewed 8.3k times · Source

How can I create a signal out of a notification name? For example, I want to go from:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(userDidChange:)
                                             name:kTTCurrentUserLoggedOffNotification
                                           object:nil];

to something like:

[signalForName(kTTCurrentUserLoggedOffNotification) subscribeNext:^(id x){
...
}];

Answer

hfossli picture hfossli · Nov 14, 2013

-[NSNotificationCenter rac_addObserverForName:object:] returns an infinite signal. You can subscribe to it like this

Objective-c

[[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil]
  takeUntil:[self rac_willDeallocSignal]]
  subscribeNext:^(id x) {
     NSLog(@"Notification received");
}];

Swift

NSNotificationCenter.defaultCenter()
  .rac_addObserverForName(UIKeyboardWillShowNotification, object: nil)
  .takeUntil(self.rac_willDeallocSignal())
  .subscribeNext { (_) in
     print("Notification received")
  }

This signal is as stated infinite. If you need this signal/subscription to be bound to the lifetime of self you can add takeUntil: with rac_willDeallocSignal like this:

Objective-c

[[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil]
  takeUntil:[self rac_willDeallocSignal]]
  subscribeNext:^(id x) {
     NSLog(@"Notification received");
}];

Swift

NSNotificationCenter.defaultCenter()
  .rac_addObserverForName(UIKeyboardWillShowNotification, object: nil)
  .takeUntil(self.rac_willDeallocSignal())
  .subscribeNext { (_) in
     print("Notification received")
  }