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){
...
}];
3 个解决方案
#1
46
-[NSNotificationCenter rac_addObserverForName:object:]
returns an infinite signal. You can subscribe to it like this
-[NSNotificationCenter rac_addObserverForName:object:]返回一个无限信号。你可以像这样订阅它
Objective-c
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:
这个信号是无限的。如果需要将此信号/订阅绑定到self的生命周期,可以添加takeUntil:与rac_willDeallocSignal如下:
Objective-c
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
10
In the RACExtensions you can find the NSNotificationCenter (RACSupport)
category. That has a method for this purpose:
在RACExtensions中,可以找到NSNotificationCenter (RACSupport)类别。为此目的有一种方法:
- (RACSignal *)rac_addObserverForName:(NSString *)notificationName
object:(id)object;
#3
-1
Swift
version using ReactiveCocoa 4.1
:
使用ReactiveCocoa的Swift版本4.1:
NSNotificationCenter.defaultCenter()
.rac_addObserverForName(UIKeyboardWillShowNotification, object: nil)
.takeUntil(self.rac_willDeallocSignal())
.subscribeNext { (_) in
print("UIKeyboardWillShowNotification")
}
#1
46
-[NSNotificationCenter rac_addObserverForName:object:]
returns an infinite signal. You can subscribe to it like this
-[NSNotificationCenter rac_addObserverForName:object:]返回一个无限信号。你可以像这样订阅它
Objective-c
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:
这个信号是无限的。如果需要将此信号/订阅绑定到self的生命周期,可以添加takeUntil:与rac_willDeallocSignal如下:
Objective-c
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
10
In the RACExtensions you can find the NSNotificationCenter (RACSupport)
category. That has a method for this purpose:
在RACExtensions中,可以找到NSNotificationCenter (RACSupport)类别。为此目的有一种方法:
- (RACSignal *)rac_addObserverForName:(NSString *)notificationName
object:(id)object;
#3
-1
Swift
version using ReactiveCocoa 4.1
:
使用ReactiveCocoa的Swift版本4.1:
NSNotificationCenter.defaultCenter()
.rac_addObserverForName(UIKeyboardWillShowNotification, object: nil)
.takeUntil(self.rac_willDeallocSignal())
.subscribeNext { (_) in
print("UIKeyboardWillShowNotification")
}