Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8855551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:09:43+00:00 2026-06-14T14:09:43+00:00

When using methods which return blocks they can be very convenient. However, when you

  • 0

When using methods which return blocks they can be very convenient.
However, when you have to string a few of them together it gets messy really quickly

for instance, you have to call 4 URLs in succession:

[remoteAPIWithURL:url1 success:^(int status){
    [remoteAPIWithURL:url2 success:^(int status){
        [remoteAPIWithURL:url3 success:^(int status){
            [remoteAPIWithURL:url2 success:^(int status){
            //succes!!!
            }];
        }];
    }];
}];

So for every iteration I go one level deeper, and I don’t even handle errors in the nested blocks yet.

It gets worse when there is an actual loop. For instance, say I want to upload a file in 100 chunks:

- (void) continueUploadWithBlockNr:(int)blockNr
{
    if(blocknr>=100) 
    {
    //success!!!
    }
    [remoteAPIUploadFile:file withBlockNr:blockNr success:^(int status)
    {
        [self continueUploadWithBlockNr:blockNr];
    }];
}

This feels very unintuitive, and gets very unreadable very quick.

In .Net they solved all this using the async and await keyword, basically unrolling these continuations into a seemingly synchronous flow.

What is the best practice in Objective C?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:09:44+00:00Added an answer on June 14, 2026 at 2:09 pm

    Your question immediately made me think of recursion. Turns out, Objective-c blocks can be used in recursion. So I came up with the following solution, which is easy to understand and can scale to N tasks pretty nicely.

    // __block declaration of the block makes it possible to call the block from within itself
    __block void (^urlFetchBlock)();
    
    // Neatly aggregate all the urls you wish to fetch
    NSArray *urlArray = @[
        [NSURL URLWithString:@"http://www.google.com"],
        [NSURL URLWithString:@"http://www.stackoverflow.com"],
        [NSURL URLWithString:@"http://www.bing.com"],
        [NSURL URLWithString:@"http://www.apple.com"]
    ];
    __block int urlIndex = 0;
    
    // the 'recursive' block 
    urlFetchBlock = [^void () {
        if (urlIndex < (int)[urlArray count]){
            [self remoteAPIWithURL:[urlArray objectAtIndex:index] 
                success:^(int theStatus){
                    urlIndex++;
                    urlFetchBlock();
                }
    
                failure:^(){
                    // handle error. 
                }];
        }
    } copy];
    
    // initiate the url requests
    urlFetchBlock();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two methods using the EF4 - one which returns a list of
Im trying to create a simple callback using blocks. I have a MainViewController which
I have a method which can be written pretty neatly through method chaining: return
I have got method which is using Assembly.LoadFrom(...) statement and returns the supported cultures
I am using reflection class to invoke some methods which are on the some
I'm using Smarty templates which call object methods. I've put the code on a
Which are the best methods for implementing replication on servers using Java RMI. I
Using some of the methods, I am able to check the orientations to which
I'm using a named-pipe WCF service, which has about 1000 methods (yes, I know
I have following method which I am using to load ActiveX control dynamically, Dim

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.