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];