本文主要是介绍ReactiveObjC 响应函数式框架 简单实用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
ReactiveObjC 响应函数式框架
ReactiveObjC 是什么?
RAC,简单来说,就是信号,提供RACSignal
捕捉当前值和将来值的信号,通过对信号进行链接,组合和反应,软件可以以声明方式编写,而不需要连续观察和更新值的代码
// When self.username changes, logs the new name to the console. // // RACObserve(self, username) creates a new RACSignal that sends the current // value of self.username, then the new value whenever it changes. // -subscribeNext: will execute the block whenever the signal sends a value. [RACObserve(self, username) subscribeNext:^(NSString *newName) {NSLog(@"%@", newName); }];
ReactiveObjC 什么时候用?
响应式编程,处理异步或事件驱动的数据源,链接依赖的操作,并行独立的工作,简化收集转换
@weakify(self); @strongify(self);
ReactiveObjC的简单使用
1.监听按钮点击事件
@weakify(self)
[[self.submitBnt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
@strongify(self)
//按钮点击后响应内容
}];
2.监听通知事件
@weakify(self)
[[[[NSNotificationCenter defaultCenter] rac_addObserverForName:KGestureLoginSueccessNotification object:nil] takeUntil:[self rac_willDeallocSignal]]subscribeNext:^(NSNotification * _Nullable x) {
@strongify(self)
//接受通知后响应内容
}];
3.监听某一对象的属性变化
宏定义RACObserve监听某个对象的某个属性的改变
[[[RACObserve(self.viewModel, page) distinctUntilChanged] deliverOnMainThread]
subscribeNext:^(id _Nullable x) {
@strongify(self)
//响应内容
}];
4.多个信号的处理
//当需要请求多个数据,在所有数据请求完成之后才进行更新UI或者其他操作。相当于多线程组
RACSignal *request1 = [RACSignal createSignal:^RACDisposable *(id<RACSubscri
这篇关于ReactiveObjC 响应函数式框架 简单实用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!