0

Basically I have an existing API manager that is blocking me from going forward. This existing manager is something I should not mess with right now.

This is the gist of my problem. I mean, I can go forward with this way, but it's sooooo annoying. I hate these nested completion blocks. Does anyone have any workaround idea that can solve this? PromiseKit is out of the option.

This is how I call it.

- (void)doEverythingHere {
    [self getDataOneWithCompletion:^(DataOneModel *response) {
            if (response.isSomething) {
                [self getDataTwoWithCompletion:^(DataOneModel *response) {
                    // And call some more of these...
                    // So the nested blocks will never end... and it's ugly
                };
            } else {
                [self getDataThreeWithCompletion:^(DataOneModel *response) {
                    // And call some more of these...
                    // So the nested blocks will never end... and it's ugly
                };
            }
    }];
}

These are the sampl API methods.

- (void)getDataOneWithCompletion:(void(^)(DataOneModel *response))completion {
    [APIManager getDataOneWithResponse:^(DataOneModel *response) {
        completion(response)
    }];
}

- (void)getDataTwoWithCompletion:(void(^)(DataTwoModel *response))completion {
    [APIManager getDataTwoWithResponse:^(DataTwoModel *response) {
        completion(response)
    }];
}

// And 4 more of these API call methods.
2
  • Yeah, it’s a tad clumsy and promises/futures are one solution. Another is custom asynchronous NSOperation subclasses with dependencies or the like, though I’m not sure it’s worth the effort. The simple solution is functional decomposition, breaking the tower into a series of individual functions, each handling the next level of nesting. Commented May 14, 2021 at 12:37
  • Sorry - I posted this as an answer first but it should be a comment. Why does it not work or block? That nesting surely does not keep on forever? Even so, it should work but I am missing something? Maybe you need to make those nested calls on a different thread to prevent the block. Commented May 15, 2021 at 10:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.