title: Reactive Cocoa and multiple AFNetworking requests in short period of time
body: <p>I'm struggeling with the way I handle multiple requests to a web service with AFNetworking and Reactive Cocoa. In this scenario the user is asking the API to deliver a bunch of suggestions for a character/integer search input to pick a city out of a list.</p>
  
  <p>Here's my code: </p>
  
  <p>First the method that gets executed as soon as the user types in more than 3 characters/integers</p>
  
  <pre><code>- (void)fetchData:(NSString *)searchText
  {
      NSLog(@"%@", searchText);
  
      if ([searchText validateStringwithPattern:BKPostcodeRegEx]) {
          self.searchURL = [NSString stringWithFormat:@"%@location/postalCode/%@/%@", BKBaseURL, self.countryCode, searchText];
      } else if ([searchText validateStringwithPattern:BKCityRegEx]) {
          self.searchURL = [NSString stringWithFormat:@"%@location/city/%@/%@", BKBaseURL, self.countryCode, searchText];
      } else {
          NSLog(@"Error Alert - No Valid Input");
          return;
      }
  
      RAC(self, searchResults) = [[[self postRequest] map:^(NSDictionary *json) {
          NSArray *results = json[@"data"][@"locations"];
          return results;
      }] catch:^(NSError *error) {
          return [RACSignal return:@[]];
      }];
  }
  </code></pre>
  
  <p>Now this is where I actually create the signal and pass it back to <code>self.searchResults</code>:</p>
  
  <pre><code>- (RACSignal *)postRequest
  {
      return [[[RACSignal createSignal:^RACDisposable *(id&lt;RACSubscriber&gt; subscriber) {
  
          [self.requestOperationManager GET:self.searchURL parameters:self.params success:^(AFHTTPRequestOperation *operation, id responseObject) {
              [subscriber sendNext:responseObject];
              [subscriber sendCompleted];
          }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              [subscriber sendError:error];
          }];
  
          return [RACDisposable disposableWithBlock:^{
              [self.requestOperationManager.operationQueue cancelAllOperations];
          }];
  
      }] doError:^(NSError *error) {
          NSLog(@"error: %@", [error description]);
      }] throttle:0.5];
  }
  </code></pre>
  
  <p>I think the problem is, that I start subscribing to the signal before the signal prior to the current signal has been completed and thus cause an exception as I'm trying to subscribe again. </p>
  
  <blockquote>
    <p><em>*</em> Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Signal  name: [[[[+createSignal:] -doError:] -throttle: 0.500000]
    -map:] -catch: is already bound to key path "searchResults" on object , adding signal
     name: [[[[+createSignal:] -doError:]
    -throttle: 0.500000] -map:] -catch: is undefined behavior'</p>
  </blockquote>
  
  <p>My guess is that I have to try something like this in the postRequest method but this doesn't seem to work out of the box:</p>
  
  <pre><code>if (self.requestOperationManager.operationQueue.operationCount == 1) {
    NSLog(@"cancel all operations");
    [self.requestOperationManager.operationQueue cancelAllOperations];
    [subscriber sendCompleted];
  }
  </code></pre>