Reputation: 4918
I want to send signal only once per 3 seconds (so if there is another signal again, skip it if previous didn't end still) There is my code :
RACSignal *textViewSignal = [self.textView.rac_textSignal throttle:3];
[textViewSignal subscribeNext:^(id x) {
[[BKSocketManager sharedManager] sendTypingRequestForChatroomId:_chatRoom.chatRoomId
success:^(Response *response) {
NSLog(@"Typing request success");
}
failure:^(NSError *error) {
NSLog(@"Typing request failed with error: %@", error);
}
tag:0];
}];
But it just send last signal if delay between them more 3 seconds.
Upvotes: 0
Views: 1025
Reputation: 750
throttle
will delay the sending of a value by the argument's time value, and not send it at all if a new value comes and replaces it before then.
If you are looking to send a value every 3 seconds, you should use
+ (RACSignal *)interval:(NSTimeInterval)interval onScheduler:(RACScheduler *)scheduler;
to set up a signal that sends a (unimportant) value every 3 seconds. Then, use
- (RACSignal *)sample:(RACSignal *)sampler;
to send the latest value from your source signal when the interval
signal sends a value.
This would look something like
RACSignal* timer = [RACSignal interval:3 onScheduler:[RACScheduler mainThreadScheduler];
RACSignal *textViewSignal = [[self.textView.rac_textSignal sample:timer] distinctUntilChanged];
[textViewSignal subscribeNext:^(id x) {
[[BKSocketManager sharedManager] sendTypingRequestForChatroomId:_chatRoom.chatRoomId
success:^(Response *response) {
NSLog(@"Typing request success");
}
failure:^(NSError *error) {
NSLog(@"Typing request failed with error: %@", error);
}
tag:0];
}];
The distinctUntilChanged
call at the end will prevent you from kicking off network requests when nothing has changed as it guarantees that it will never send two consecutive values that are equal to each other.
Upvotes: 1