HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Multiple serial URL requests in Objective-C / iOS

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
iosserialobjectivemultipleurlrequests

Problem

I want to check the existence of various API endpoints by doing serial URL request. If the first one fails, I want to try the second one, if the second fails I want to try the third one, etc.

Here is some code I use to do this:

self.apiEndpoint = kApiAjaxEndpoint;
[self testEndpointWithBlock:^(id response, NSError *error) {
    if (error) {
        self.apiEndpoint = kApiAjaxLegacyEndpoint;
        [self testEndpointWithBlock:^(id response, NSError *error) {
            if (error) {
                self.apiEndpoint = kApiRestEndpoint;
            }
            [self saveApiEndpoint];
        }];
    } else {
        [self saveApiEndpoint];
    }
}];


So for now I'm only testing for 2 API endpoints but the moment I add a new endpoint I would need to add another call to testEndpointWithBlock: with error handling code.

I was wondering if you can think of a more elegant way of doing this?

Solution


  • Create several instances of NSOperation



  • Set dependencies so that one runs after another.



  • Add to a private NSOperationQueue.



  • Cancel all operations in a queue if a match is found.

Context

StackExchange Code Review Q#36373, answer score: 3

Revisions (0)

No revisions yet.