ReactiveCocoa使用簡(jiǎn)述

RAC有一個(gè)主要的優(yōu)點(diǎn),就是提供一個(gè)單一的,統(tǒng)一的方法去處理異步的行為,包括delegate方法,blocks回調(diào),target-action機(jī)制,notification和KVO。
KVO:
[RACObserver(self,username) subscribeNext:^(NSString *newName){
NSLog(@"%@",newName);
}];
當(dāng)userName屬性變化的時(shí)候會(huì)調(diào)用。還可以添加篩選:
[RACObserve(self,username)
filter:^(NSString *newName){
return [newName hasPreFix:@"j"];
}]
subscribeNext:^(NSString *newName){
NSLog(@"%@",newName);
}];

可以使用combineLatest:reduce來(lái)結(jié)合兩個(gè)信號(hào):
RAC(self,createEnabled) = [RACSignal combineLatest:@[RACObserve(self,password),RACObserve(self,passwordConfirmation)]
reduce:^(NSString *password,NSString *passwordConfirm){
return @([passwordConfirm isEqualToString:password]);
}];

表示按鈕點(diǎn)擊:
self.button.rac_command = [[RACCommand alloc] initWithSignalBlock:^(id _){
NSLog(@"button was pressed");
return [RACSignal empty];
}];

異步的網(wǎng)絡(luò)操作:
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;

對(duì)于使用signals進(jìn)行一步操作,通過(guò)連接和改變這些signals能夠進(jìn)行更加復(fù)雜的行為,在一組操作完成時(shí),工作能夠很簡(jiǎn)單觸發(fā):
比如,兩個(gè)網(wǎng)絡(luò)請(qǐng)求一起,都完成之后才進(jìn)行操作:
[[RACSignal
merge:@[[client fetchUserRepos],[client fetchOrgRepos] ]]
subscribeCompleted:^{
NSLog(@"They are both done!");
}];
順序的執(zhí)行異步操作:
// 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.");
}];

綁定異步操作的結(jié)果:
// 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];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容