简体   繁体   中英

Reactive Cocoa `then` operator

I couldn't find much documentation about RAC then operator. What is the purpose it serves. When should I use. it? Can someone explain in below context?

[[[[self requestAccessToTwitterSignal]
  then:^RACSignal *{
    @strongify(self)
    return self.searchText.rac_textSignal;
  }]
  filter:^BOOL(NSString *text) {
    @strongify(self)
    return [self isValidSearchText:text];
  }]
  subscribeNext:^(id x) {
    NSLog(@"%@", x);
  } error:^(NSError *error) {
    NSLog(@"An error occurred: %@", error);
  }];

It essentially just makes the signal you apply it to work as a delay, it won't send next events until the original signal completes, at which point it'll switch to the signal you supplied with then: .

requestAccessToTwitterSignal will do something stateful and set a twitter token of some kind, so we use then: to wait until it's done and return a new signal that we want to get next events from.

The code documentation provide a sound explanation.

/// Ignores all `next`s from the receiver, waits for the receiver to complete,
/// then subscribes to a new signal.
///
/// block - A block which will create or obtain a new signal to subscribe to,
///         executed only after the receiver completes. This block must not be
///         nil, and it must not return a nil signal.
///
/// Returns a signal which will pass through the events of the signal created in
/// `block`. If the receiver errors out, the returned signal will error as well.

So in your context. it waits until requestAccessToTwitterSignal complete. Ignores any event after that. And then subscribes to the new signal. Which is self.searchText.rac_textSignal . The filter applies to the new signal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM