ReactiveCocoa使用简述

RAC有一个主要的优点,就是提供一个单一的,统一的方法去处理异步的行为,包括delegate方法,blocks回调,target-action机制,notification和KVO。 KVO:

[RACObserver(self,username) subscribeNext:^(NSString *newName){
NSLog(@"%@",newName);
}];
当userName属性变化的时候会调用。还可以添加筛选:
[RACObserve(self,username) 
      filter:^(NSString *newName){
          return [newName hasPreFix:@"j"];
      }]
      subscribeNext:^(NSString *newName){
            NSLog(@"%@",newName);
}];
复制代码

可以使用combineLatest:reduce来结合两个信号:

RAC(self,createEnabled) = [RACSignal  combineLatest:@[RACObserve(self,password),RACObserve(self,passwordConfirmation)] 
reduce:^(NSString *password,NSString *passwordConfirm){
      return @([passwordConfirm isEqualToString:password]);
}];
复制代码

表示按钮点击:

self.button.rac_command = [[RACCommand alloc] initWithSignalBlock:^(id _){
NSLog(@"button was pressed");
return [RACSignal empty];
}];

异步的网络操作:
self.loginCommand = [[RACCommand alloc]  
      initWithSignalBlock:^(id sender){
          return [client logIn];
}];
[self.loginCommand.executionSignal subscribeNext:^(RACSignal *loginSignal){
      [loginSignal subscribeCompleted:^{
              NSLog(@"Logged in successfully!");
}];
}];
self.loginButton.rac_command = self.loginCommand;
复制代码

对于使用signals进行一步操作,通过连接和改变这些signals能够进行更加复杂的行为,在一组操作完成时,工作能够很简单触发: 比如,两个网络请求一起,都完成之后才进行操作:

[[RACSignal
 merge:@[[client fetchUserRepos],[client fetchOrgRepos] ]]
subscribeCompleted:^{
      NSLog(@"They are both done!");
}];
顺序的执行异步操作:
// Logs in the user, then loads any cached messages, then fetches the remaining
// messages from the server. After that's all done, logs a message to the
// console.
//
// The hypothetical -logInUser methods returns a signal that completes after
// logging in.
//
// -flattenMap: will execute its block whenever the signal sends a value, and
// returns a new RACSignal that merges all of the signals returned from the block
// into a single signal.
[[[[client 
    logInUser] 
    flattenMap:^(User *user) {
        // Return a signal that loads cached messages for the user.
        return [client loadCachedMessagesForUser:user];
    }]
    flattenMap:^(NSArray *messages) {
        // Return a signal that fetches any remaining messages.
        return [client fetchMessagesAfterMessage:messages.lastObject];
    }]
    subscribeNext:^(NSArray *newMessages) {
        NSLog(@"New messages: %@", newMessages);
    } completed:^{
        NSLog(@"Fetched all messages.");
    }];
复制代码

绑定异步操作的结果:

// Creates a one-way binding so that self.imageView.image will be set as the user's
// avatar as soon as it's downloaded.
//
// The hypothetical -fetchUserWithUsername: method returns a signal which sends
// the user.
//
// -deliverOn: creates new signals that will do their work on other queues. In
// this example, it's used to move work to a background queue and then back to the main thread.
//
// -map: calls its block with each user that's fetched and returns a new
// RACSignal that sends values returned from the block.
RAC(self.imageView, image) = [[[[client 
    fetchUserWithUsername:@"joshaber"]
    deliverOn:[RACScheduler scheduler]]
    map:^(User *user) {
        // Download the avatar (this is done on a background queue).
        return [[NSImage alloc] initWithContentsOfURL:user.avatarURL];
    }]
    // Now the assignment will be done on the main thread.
    deliverOn:RACScheduler.mainThreadScheduler];
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值