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 7869063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:11:49+00:00 2026-06-03T01:11:49+00:00

I’ve just recently switched from ASIHttpRequest library to AFNetworking. I really like the simplicity,

  • 0

I’ve just recently switched from ASIHttpRequest library to AFNetworking. I really like the simplicity, but I am still struggling to understand how to structure my async code.

Please consider this sign up scenario.

  • First I want to check if the entered email adresse is available.
  • Next I want to check if the entered username is available.
  • If both the above is valid and available I want to submit my real signup request.

My code would be looking something like.

- (void)signUp{

    BOOL hasValidEmail = [self validateEmail:email];
    BOOL hasValidUsername = [self validateUsername:username];

    if(!hasValidEmail){
        NSLog(@"Invalid email");
        return;
    }

    if(!hasValidUsername){
        NSLog(@"Invalid username");
        return;
    }

    if (hasValidEmail && hasValidUsername) {
        NSLog(@"Go ahead and create account");
    }
}

I’m not quite sure how to structure this considering the async nature of my networking methods. Often last condition will be reached before the two previous availability checks have received their response.

The availability checking methods would look something like:

- (BOOL)validateEmail:(NSString*)email{

    __block NSString* emailAlreadyExists = @"";

    NSString* url = [NSString stringWithFormat:@"user/emailexists/%@", email];

    [[APIClient sharedClient] getPath:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        emailAlreadyExists = [responseObject valueForKey:@"exists"];

    } failure:^(AFHTTPRequestOperation *operation, NSError* error) {
        NSLog(@"Email availability check failed: %@", error.localizedDescription);
    }];

    if([emailAlreadyExists isEqualToString:@"true"]){
        return NO;
    }

    return YES;   
}

Maybe it’s just my blocks skills that needs improving, but I’d really like to hear how you would you structure a scenario like this?

Though code samples would be “nice”, I’m really looking for patterns or good techniques you know of.

Thanks.

  • 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-03T01:11:50+00:00Added an answer on June 3, 2026 at 1:11 am

    I usually break these things into steps and start the next step when the previous one succeeds. Blocks are great to pass along for this purpose.

    This obviously wont compile, but hopefully it can give you some idea of how to do this:

    typedef enum
    {
        SignupErrorNetworkError,
        SignupErrorEmailAlreadyTaken,
        SignupErrorUsernameAlreadyTaken,
    } SignupError;
    
    typedef enum
    {
        // These steps must be performed in this order.
        SignupStepValidateEmail,
        SignupStepValidateUsername,
        SignupStepCreateAccount,
    } SignupStep;
    
    typedef void (^SignupSuccessBlock)();
    typedef void (^SignupFailureBlock)(SignupError reason);
    
    // Call this to sign up.
    - (void)signupWithSuccess:(SignupSuccessBlock)success failure:(SignupFailureBlock)failure
    {
        // Start the first step of the process.
        [self performSignupStep:SignupStepValidateEmail success:success failure:failure];
    }
    
    // Internal method. Don't call this from outside.
    - (void)performSignupStep:(SignupStep)step success:(SignupSuccessBlock)success failure:(SignupFailureBlock)failure
    {
        switch (step)
        {
            case SignupStepValidateEmail:
                [[APIClient sharedClient] getPath:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
                    if ([responseObject valueForKey:@"exists"])
                    {
                        if (failure) failure(SignupErrorEmailAlreadyTaken);
                        return;                }
                    }
                    // Start next async step in signup process.
                    [self performSignupStep:SignupStepValidateUsername success:success failure:failure];
                } failure:^(AFHTTPRequestOperation *operation, NSError* error) {
                    if (failure) failure(SignupErrorNetworkError);
                }];
                break;
            case SignupStepValidateUsername:
                // Similar to the step above. Starts the create account step on success.
                break;            
            case SignupStepCreateAccount:
                // Similar to the step above. Call the success block when done.
                break;            
        }
    }
    

    If the switch is getting too long and ugly you could also make the steps into separate methods and delete the step-enum: validateEmailWithSuccess:failure which continues by calling validateUsernameWithSuccess:failure etc.

    I just wanted to emphasize the state machine nature of the process in the example above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I have a French site that I want to parse, but am running into

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.