ReactiveCocoa使用簡述

chenshipeng發表於2017-12-22

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];
複製程式碼

相關文章