Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an Objective-C Completion Block

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.

(I think what I need is a completion block, but please correct me if I'm wrong.)

Here is the situation:

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.

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.

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)

[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. }]; 

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)

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]; } 

Please let me know if you need additional information or clarification.

Thank you in advance!

--------- EDIT ONE ----------

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.

Method Call:

[testOrg registerNewUserOrgWithUser:currentUser          creatingOrgContactWithName:@"MyBigHappy Corp."                           withBlock:^(BOOL succeeded, NSError *error) {                               if (error) {                                   NSLog(@"Not working");                               } else {                                   NSLog(@"Working!");                               }                           }]; 

Method Implementation:

@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:&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:&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:&saveUserContactError];          if (saveUserContactError) {             [errors addObject:@"Saving user contact failed"];         }          NSError *saveUserError = nil;         [user setParentOrg:self];         [user setUserContact:userContact];         [user save:&saveUserError];          if (saveUserError) {             [errors addObject:@"Saving User failed"];         }          // Return if block succeeded and any errors.         NSError *error = nil;         BOOL succeeded;         if (errors.count > 0) {              NSDictionary *userInfo = @{@"error" : errors};             errors = [NSError errorWithDomain:@"MyAppErrorDomain"                                          code:1                                      userInfo:userInfo];             succeeded = NO;         } else {             succeeded = YES;         }         block(succeeded, error);     });  }  @end 
like image 439
Andrew Avatar asked Jan 29 '14 16:01

Andrew


People also ask

How do you create a completion block in Objective C?

Define Block Types - (void) performWithCompletion: (void (^)(BOOL finished)) completionBlock; Alternatively you simplify matters by declaring a custom block type in your class header file. Here's an example that defines a type and then uses it in a method signature identical to the example you just saw.

What is completion block in Objective C?

A completion handler is nothing more than a simple block declaration passed as a parameter to a method that needs to make a callback at a later time.

What is a completion block?

The block to execute after the operation's main task is completed.


1 Answers

I always use this when I want to write a block:

http://fuckingblocksyntax.com

EDIT

If you are writing Swift then use this:

http://fuckingswiftblocksyntax.com

If the above links are not opening because of the obscene language, use this one.

http://goshdarnblocksyntax.com/

like image 199
CW0007007 Avatar answered Sep 17 '22 18:09

CW0007007