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