title: How to write an Objective-C Completion Block
body: <p>I'm in a situation where need to call a class method from my view controller, have it do it's thing, but then perform some actions ONLY AFTER the class method has completed.</p>
  
  <p>(I think what I need is a completion block, but please correct me if I'm wrong.)</p>
  
  <p>Here is the situation:</p>
  
  <p>I'm using Parse.com for my apps back end. When a user signs up for an account, they enter their name, company and some other info in a popup then click submit. The submit button is linked to a class method (shown below) which takes their PFUser object, and company name and creates some database objects. After the function completes, the popup is dismissed using a delegate.</p>
  
  <p>The issue is I need the creation of these objects to happen in a specific order because they depend on each others objectId's to exist. The problem is, the delegate method to dismiss the popup is called right away after clicking submit as it's the next on the stack.</p>
  
  <p>When saving a Parse object one calls a method that looks something like this:
  (This is kind of what I'm hoping to write and I think would solve my issue)</p>
  
  <pre><code>[someParseObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
      // Code here runs AFTER the method completes.
      // This also happens on another thread which
      // I'd like to implement as well.
  }];
  </code></pre>
  
  <p>So, What I need to figure out how to do something like the following:
  (Everything having to do with the block is completely wrong I'm sure)</p>
  
  <pre><code>SignUpViewController.m
  
  myUserOrg *userOrg = [myUserOrg object]; // myUserOrg = Custom PFObject Subclass
  
  // My method that takes in a user object and a string, creates
  // the database objects in order.
  [userOrg registerNewUserOrgWithUser:(PFUser*) andCompanyName:(NSString*) companyName withBlock(somethingHere)block {
  
      if(error) {
          NSLog(@"Unable to create org!");
      } else {
          NSLog(@"Created Org!");
          [self.delegate dismissSignupView];
  }
  </code></pre>
  
  <p>Please let me know if you need additional information or clarification.</p>
  
  <p>Thank you in advance!</p>
  
  <p>--------- EDIT ONE ----------</p>
  
  <p>Alright, So several sizeable units of time later, this is what I've come up with. The whole implementation could be better simplified and make far fewer api calls but will work on that. Several other glaring issues with it as well but is a first step.</p>
  
  <p>Method Call:</p>
  
  <pre><code>[testOrg registerNewUserOrgWithUser:currentUser
           creatingOrgContactWithName:@"MyBigHappy Corp."
                            withBlock:^(BOOL succeeded, NSError *error) {
                                if (error) {
                                    NSLog(@"Not working");
                                } else {
                                    NSLog(@"Working!");
                                }
                            }];
  </code></pre>
  
  <p>Method Implementation:</p>
  
  <pre><code>@implementation MYUserOrg
  
  @dynamic orgContact;
  @dynamic orgDisplayName;
  @dynamic members;
  @dynamic contacts;
  
  + (NSString *)parseClassName {
      return @"MYUserOrg";
  }
  
  dispatch_queue_t NewUserOrgRegistrationQueue;
  
  -(void)registerNewUserOrgWithUser:(MYUser*)user
         creatingOrgContactWithName:(NSString*) orgContactName
                          withBlock:(MYBooleanResultBlock) block {
  
      NewUserOrgRegistrationQueue = dispatch_queue_create("com.myapp.initialOrgCreationQueue", NULL);
  
      dispatch_async(NewUserOrgRegistrationQueue, ^{
  
          NSMutableArray *errors = [[NSMutableArray alloc] init];
  
          // Initial org save to generate objectId
          NSError *orgSaveError = nil;
          [self save:&amp;orgSaveError];
  
          if (orgSaveError) {
              [errors addObject:@"Initial Org save Failed"];
          }
  
          // Create and Relate Org Contact
          NSError *saveOrgContactError = nil;
          MYontact *orgContact = [MYContact object];
          [orgContact setContactType:MYContactTypeUserOrganization];
          [orgContact setDisplayName:orgContactName];
          [orgContact setParentOrg:self];
          [orgContact save:&amp;saveOrgContactError];
  
          if (saveOrgContactError) {
              [errors addObject:@"Saving Org Contact Failed"];
          } else {
              // If Org contact saved, set it;
              [self setOrgContact:orgContact];
          }
  
          // Create amd Relate User Contact
          NSError *saveUserContactError = nil;
          MYContact *userContact = [MYContact object];
          [userContact setFirstName:user.firstName];
          [userContact setLastName:user.lastName];
          [userContact setContactType:MYcontactTypeUser];
          [userContact setParentOrg:self];
          [userContact save:&amp;saveUserContactError];
  
          if (saveUserContactError) {
              [errors addObject:@"Saving user contact failed"];
          }
  
          NSError *saveUserError = nil;
          [user setParentOrg:self];
          [user setUserContact:userContact];
          [user save:&amp;saveUserError];
  
          if (saveUserError) {
              [errors addObject:@"Saving User failed"];
          }
  
          // Return if block succeeded and any errors.
          NSError *error = nil;
          BOOL succeeded;
          if (errors.count &gt; 0) {
  
              NSDictionary *userInfo = @{@"error" : errors};
              errors = [NSError errorWithDomain:@"MyAppErrorDomain"
                                           code:1
                                       userInfo:userInfo];
              succeeded = NO;
          } else {
              succeeded = YES;
          }
          block(succeeded, error);
      });
  
  }
  
  @end
  </code></pre>