说说ReactiveCocoa 2

- [RACObserve(self, username) subscribeNext: ^(NSString *newName){
- NSLog(@"newName:%@", newName);
- }];
- RAC(self.logInButton, enabled) = [RACSignal
- combineLatest:@[
- self.usernameTextField.rac_textSignal,
- self.passwordTextField.rac_textSignal,
- RACObserve(LoginManager.sharedManager, loggingIn),
- RACObserve(self, loggedIn)
- ] reduce:^(NSString *username, NSString *password, NSNumber *loggingIn, NSNumber *loggedIn) {
- return @(username.length > 0 && password.length > 0 && !loggingIn.boolValue && !loggedIn.boolValue);
- }];
- RACSignal *signal = [RACSignal createSignal:^ RACDisposable * (id<RACSubscriber> subscriber) {
- NSLog(@"triggered");
- [subscriber sendNext:@"foobar"];
- [subscriber sendCompleted];
- return nil;
- }];
- [signal subscribeCompleted:^{
- NSLog(@"subscription %u", subscriptions);
- }];
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"Alert" delegate:nil cancelButtonTitle:@"YES" otherButtonTitles:@"NO", nil];
- [[alertView rac_buttonClickedSignal] subscribeNext:^(NSNumber *indexNumber) {
- if ([indexNumber intValue] == 1) {
- NSLog(@"you touched NO");
- } else {
- NSLog(@"you touched YES");
- }
- }];
- [alertView show];
- [[[self.cancelButton
- rac_signalForControlEvents:UIControlEventTouchUpInside]
- takeUntil:self.rac_prepareForReuseSignal]
- subscribeNext:^(UIButton *x) {
- // do other things
- }];
- voteButton.rac_command = [[RACCommand alloc] initWithEnabled:self.viewModel.voteCommand.enabled signalBlock:^RACSignal *(id input) {
- // Assume that we're logged in at first. We'll replace this signal later if not.
- RACSignal *authSignal = [RACSignal empty];
- if ([[PXRequest apiHelper] authMode] == PXAPIHelperModeNoAuth) {
- // Not logged in. Replace signal.
- authSignal = [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
- @strongify(self);
- FRPLoginViewController *viewController = [[FRPLoginViewController alloc] initWithNibName:@"FRPLoginViewController" bundle:nil];
- UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
- [self presentViewController:navigationController animated:YES completion:^{
- [subscriber sendCompleted];
- }];
- return nil;
- }]];
- }
- return [authSignal then:^RACSignal *{
- @strongify(self);
- return [[self.viewModel.voteCommand execute:nil] ignoreValues];
- }];
- }];
- [voteButton.rac_command.errors subscribeNext:^(id x) {
- [x subscribeNext:^(NSError *error) {
- [SVProgressHUD showErrorWithStatus:[error localizedDescription]];
- }];
- }];
- [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"MyNotification" object:nil] subscribeNext:^(NSNotification *notification) {
- NSLog(@"Notification Received");
- }];
- NSArray *array = @[@"foo"];
- [[array rac_willDeallocSignal] subscribeCompleted:^{
- NSLog(@"oops, i will be gone");
- }];
- array = nil;
- - (void)test
- {
- RACSignal *signalA = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
- double delayInSeconds = 2.0;
- dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
- dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
- [subscriber sendNext:@"A"];
- });
- return nil;
- }];
- RACSignal *signalB = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
- [subscriber sendNext:@"B"];
- [subscriber sendNext:@"Another B"];
- [subscriber sendCompleted];
- return nil;
- }];
- [self rac_liftSelector:@selector(doA:withB:) withSignals:signalA, signalB, nil];
- }
- - (void)doA:(NSString *)A withB:(NSString *)B
- {
- NSLog(@"A:%@ and B:%@", A, B);
- }

RAC的作者之一 jspahrsummers 的一个项目
Essentilas: Understanding and Using RACCommand 介绍了RACCommand的使用,同时也涉及了RAC相关的一些点。
OAuth Token Refresh Using ReactiveCocoa 这篇文章讲了如何使用RAC来透明地获取Access Token,然后继续发送请求。
说说ReactiveCocoa 2的更多相关文章
- iOS开发之ReactiveCocoa下的MVVM(干货分享)
最近工作比较忙,但还是出来更新博客了,今天给大家分享一些ReactiveCocoa以及MVVM的一些东西,干活还是比较足的.在之前发表过一篇博文,名字叫做<iOS开发之浅谈MVVM的架构设计与团 ...
- ReactiveCocoa 冷热订阅(cold subscribe, hot subscribe)
ReactiveCocoa支持两种订阅方式,一种是冷订阅,一种是热订阅. 热订阅的特点: 1.不管有没有消息订阅着,发送者总会把消息发出去. 2.不管订阅者是什么时候订阅的,发送者总是会把相同的消息发 ...
- 为什么ReactiveCocoa中推荐使用RACSignal来做信号处理而不是RACSubject
原文解释在这里http://cocoadocs.org/docsets/ReactiveCocoa/0.6.0/ 在标题Creating hot subscribables 底下 先贴原文: The ...
- 最快让你上手ReactiveCocoa之基础篇
前言 很多blog都说ReactiveCocoa好用,然后各种秀自己如何灵活运用ReactiveCocoa,但是感觉真正缺少的是一篇如何学习ReactiveCocoa的文章,这里介绍一下. 1.Rea ...
- [iOS]ReactiveCocoa安装方法
1. 替换Ruby镜像 我们想要使用CocoaPods来安装ReactiveCocoa.由于OS X上的Ruby镜像被墙了,感谢淘宝为我们提供了国内访问镜像. $ gem sources --remo ...
- ReactiveCocoa源码拆分解析(七)
(整个关于ReactiveCocoa的代码工程可以在https://github.com/qianhongqiang/QHQReactive下载) 在这篇博客中,我将把ReactiveCocoa中的擦 ...
- ReactiveCocoa源码拆分解析(六)
(整个关于ReactiveCocoa的代码工程可以在https://github.com/qianhongqiang/QHQReactive下载) RAC为了实现优雅的信号绑定,可谓使尽浑身解数,不仅 ...
- ReactiveCocoa源码拆分解析(五)
(整个关于ReactiveCocoa的代码工程可以在https://github.com/qianhongqiang/QHQReactive下载) 好多天没写东西了,今天继续.主要讲解RAC如何于UI ...
- ReactiveCocoa源码拆分解析(四)
(整个关于ReactiveCocoa的代码工程可以在https://github.com/qianhongqiang/QHQReactive下载) 上一章节简要的说明了如何实现的热信号.但是像那么写, ...
- ReactiveCocoa源码拆分解析(三)
(整个关于ReactiveCocoa的代码工程可以在https://github.com/qianhongqiang/QHQReactive下载) 这一章节主要讨论信号的“冷”与“热” 在RAC的世界 ...
随机推荐
- 数据结构(c语言版,严蔚敏)第3章栈和队列
第3章栈和队列
- php手册的一些思考
函数手册一定要认真看,很多用法都不太清楚: array array_merge ( array $array1 [, array $... ] ) array_merge() 将一个或多个数组的单 ...
- DNS排查技术图谱
# DNS排查技术图谱 ## 应用程序视角- 应用程序 - 浏览器 - hostname cache - ping- 操作系统 - hostname cache - 域名解析器 - dig domai ...
- error LNK2001: 无法解析的外部符号 __imp__RegEnumKeyExA@32
错误: error LNK2001: 无法解析的外部符号 __imp__OpenProcessToken@12 error LNK2001: 无法解析的外部符号 __imp__LookupPrivil ...
- ASP.NET Core学习——5
日志(Logging)ASP.NET Core内建支持日志,也允许开发人员轻松切换为他们想用的其他日志框架. 通过dependency-injection请求ILoggerFactory或ILogge ...
- Conversion Specifiers and the Resulting Printed Output
Conversion Specification Output %a Floating-point number, hexadecimal digits and p-notation (C99). % ...
- Tomcat运行错误示例二
Tomcat运行错误示例二 当遇到这种错误时,一般是构建路径的问题,按步骤来就好.如图: 点击---->库---->Add Library---->下一步---->选择tomc ...
- PHP面试 PHP基础知识 十(网络协议)
网络协议 HTTP协议状态码 状态分为五大类:1XX.2XX.3XX.4XX.5XX 1XX:信息类状态码 表示接受请求正在处理 2XX:success 成功状态码 请求正常处理完毕 3XX:重定 ...
- Quartus II 使用 modelsim 仿真
转自:http://www.cnblogs.com/emouse/archive/2012/07/08/2581223.html Quartus 中调用modelsim的流程 1. 设定仿真工具 as ...
- 【二叉树】二叉树常用算法的C++实现
常见算法有: 1.求二叉树的最大深度 2.求二叉树的最小深度 3.二叉树的层次遍历 4.二叉树的前序遍历 5.二叉树的中序遍历 6.二叉树的后序遍历 7.求二叉树的节点个数 8.求二叉树的叶节点个数 ...