0

I have implemented function to download my object ELRun from the server. My code snippet:

- (void)_requestUserReservation {
    [[[networkManager requestRun] subscribeNext:^(ELRun *run) {
        [self _setupRun:run];
    } error:^(NSError *error) {
        //FIXME: renew reservation request here?
        NSLog(@"Couldn't receive user reservation");
    }];
}

Sometimes I receive an error - then I would like to retry download 2 more times. How should I achieve that using Reactive Cocoa?

I found similar question here, but I have additional next: event that I have to handle. I tried the code below but it doesn't work:

static int retries = 0;
RACSignal *apiCall = [RACSignal defer:^{
    return [self _dynamicClient] requestRun];
}];
return [[apiCall
    catch:^(NSError *error) {
        retries++;
        if (retries < 2)
            return apiCall;
        else 
            return [RACSignal empty];
    replay];

Any help or suggeestions will be appreciated!

Community
  • 1
  • 1
Szu
  • 2,254
  • 1
  • 21
  • 37

1 Answers1

1

How about introducing some recursion:

- (void)_requestUserReservation:(NSUInteger)attempt {

  [[[networkManager requestRun] subscribeNext:^(ELRun *run) {

    [self _setupRun:run];

  } error:^(NSError *error) {

      if(attempt > 2) {
        NSLog(@"Couldn't receive user reservation");
      } else {
        [self _requestUserReservation:++attempt];
      }
}];

}

Menno
  • 1,232
  • 12
  • 23