Use of combinations in RAC

  1. concat
  2. then
  3. merge
  4. zipWith
  5. combineLatest
  6. reduce

1. concat

RACSignal signalA = [RACSignal createSignal:^RACDisposable _Nullable(id _Nonnull subscriber) {
NSLog(@"request A");
[subscriber sendNext:@"A Data"];
// A Completed
[subscriber sendCompleted];
return nil;
}];
RACSignal signalB = [RACSignal createSignal:^RACDisposable _Nullable(id _Nonnull subscriber) {
NSLog(@"request B");
[subscriber sendNext:@"B Data"];
// B Completed
[subscriber sendCompleted];
return nil;
}];
// concat: Connect sequentially
// Create Combination Signal
RACSignal *concatSignal = [signalA concat:signalB];
// Subscription Combination Signal
// Note: The first signal must call sendCompleted
[concatSignal subscribeNext:^(id _Nullable x) {
NSLog(@"%@", x);
}];

2. then

RACSignal signalA = [RACSignal createSignal:^RACDisposable _Nullable(id _Nonnull subscriber) {
NSLog(@"request A");
[subscriber sendNext:@"A Data"];
// A Completed
[subscriber sendCompleted];
return nil;
}];
RACSignal signalB = [RACSignal createSignal:^RACDisposable _Nullable(id _Nonnull subscriber) {
NSLog(@"request B");
[subscriber sendNext:@"B Data"];
// B Completed
[subscriber sendCompleted];
return nil;
}];
// The first part of the feedback is discarded, only the last part of the data is available
RACSignal thenSignal = [signalA then:^RACSignal _Nonnull{
return signalB;
}];
[thenSignal subscribeNext:^(id _Nullable x) {
NSLog(@"%@", x);
}];

3. merge:

RACSubject *signalA = [RACSubject subject];
RACSubject *signalB = [RACSubject subject];
// Combined signal
RACSignal *mergeSignal = [signalA merge:signalB];
// Subscribe
[mergeSignal subscribeNext:^(id _Nullable x) {
NSLog(@"%@", x);
}];
// A combined subscription is performed for either of the two signals that send data
[signalA sendNext:@"A"];
[signalB sendNext:@"B"];

4. zipWith:

Compress two signals into one.Combination signals can only be triggered when both signals emit the signal content at the same time.And combine the data sent by the two signals into one tuple.
The block in the combined signal will not be executed until all the signals in the combined signal have been sent.

RACSubject *signalA = [RACSubject subject];
RACSubject *signalB = [RACSubject subject];
// Combined signal
RACSignal *zipSignal = [signalA zipWith:signalB];
// Subscribe
[zipSignal subscribeNext:^(id _Nullable x) {
NSLog(@"%@", x);
}];
[signalA sendNext:@"A"];
[signalB sendNext:@"B"];

combineLatest combined with reduce

  • combineLatest: Combines multiple signals and gets the latest value of each signal. Each combined signal must have sendNext at least once to trigger the combined signal.
  • Aggregation: A tuple used to aggregate the values of the signaling tuple into a single value when used for signaling.

// Create Combination Signal
// ReducBlock parameter: related to the combined signal, and one-to-one correspondence
RACSignal *combineSignal = [RACSignal combineLatest:@[_pwdFiled.rac_textSignal, _accountField.rac_textSignal] reduce:^id(NSString *pwd, NSString *account){
// block: Called whenever the source signal sends content, grouped into a new value
// The aggregated value is the content of the combined signal
NSLog(@"%@ %@", account, pwd);
return @(account.length && pwd.length);
}];
// Subscription Signal
[combineSignal subscribeNext:^(id _Nullable x) {
_loginBtn.enabled = [x boolValue];
}];
// Subscribe in macro mode
RAC(_loginBtn, enabled) = combineSignal;

Posted on Fri, 17 Jul 2020 12:37:47 -0400 by dan1956