How to use Reactive Cocoa with notifications

一笑奈何 提交于 2019-12-31 09:01:14

问题


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){
...
}];

回答1:


-[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")
  }



回答2:


In the RACExtensions you can find the NSNotificationCenter (RACSupport) category. That has a method for this purpose:

- (RACSignal *)rac_addObserverForName:(NSString *)notificationName
                               object:(id)object;



回答3:


Swift version using ReactiveCocoa 4.1:

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


来源:https://stackoverflow.com/questions/18181803/how-to-use-reactive-cocoa-with-notifications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!