0

I have read through the Apple documentation on Blocks but I am not quite sure how to use them in my situation. Within my application I have a Game Model. This has a method which creates a game of two users.

From a view controller I call the method in the game model, and once the game is created I need to make a callback to my view controller to say it was successful and a new VC can be pushed.

Current Code

Game Model

+(void)createNewGameAgainst:(PFUser *)user2 {
    NSLog(@"createNewGameAgainst");
    // First we put a HUD up for the user on the window
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];
    [[UIApplication sharedApplication].keyWindow addSubview:HUD];
    HUD.labelText = NSLocalizedString(@"HUDCreateNewGame", @"HUD - Create New Game text");
    HUD.removeFromSuperViewOnHide = YES;

    [HUD showAnimated:YES whileExecutingBlock:^{
        // Do something

        PFObject *newGame = [PFObject objectWithClassName:@"Game"];
        [newGame setObject:[PFUser currentUser] forKey:kMESGameUser1];
        [newGame setObject:user2 forKey:kMESGameUser2];
        [newGame saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                NSLog(@"succeeded game creation");
            }
        }];

    }];

    NSLog(@"end of createNewGameAgainst");
}

View Controller call

[MESGameModel createNewGameAgainst:self.gameNewOpponentuser];

I need to know how to update my method above to have a callback Block. Then update the view controller call to the method so it can execute/push a new view controller if the creation was successful.

jscs
  • 63,694
  • 13
  • 151
  • 195
StuartM
  • 6,743
  • 18
  • 84
  • 160
  • I also checked out that question before hand and was unable to determine the answer to my question. thanks – StuartM Jul 03 '13 at 21:00
  • Please be more clear about what you've tried and what _exactly_ you don't understand, then. – jscs Jul 03 '13 at 21:40

2 Answers2

3

You could essentially do something like this:

Header:

+(void)createNewGameAgainst:(PFUser *)user2 withCompletion:(void (^)(BOOL success))completionHandler;

Implementation:

+(void)createNewGameAgainst:(PFUser *)user2 withCompletion:(void (^)(BOOL success))completionHandler{
NSLog(@"createNewGameAgainst");
// First we put a HUD up for the user on the window
MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow];
[[UIApplication sharedApplication].keyWindow addSubview:HUD];
HUD.labelText = NSLocalizedString(@"HUDCreateNewGame", @"HUD - Create New Game text");
HUD.removeFromSuperViewOnHide = YES;

[HUD showAnimated:YES whileExecutingBlock:^{
    // Do something

    PFObject *newGame = [PFObject objectWithClassName:@"Game"];
    [newGame setObject:[PFUser currentUser] forKey:kMESGameUser1];
    [newGame setObject:user2 forKey:kMESGameUser2];
    [newGame saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"succeeded game creation");
        }
        completionHandler(succeeded);
    }];

}];

NSLog(@"end of createNewGameAgainst");
}

Once you have a status update, you call the completion handler with the status.

[MESGameModel createNewGameAgainst:self.gameNewOpponentuser withCompletion:^(BOOL success) {
if(success)
    //Do something wonderful
else
    //Ohs nos!
}];

When your completion handler is called, you can check to see whether or not the action was completed successfully.

TheRobDay
  • 511
  • 3
  • 7
  • Thanks this is great, however I get a warning on the method - Conflicting parameter types '__strong id' vs 'void(^_strong)(BOOL)' ? – StuartM Jul 03 '13 at 21:19
  • How should the method be named in interface? +(void)createNewGameAgainst:(PFUser *)user2 withCompletion:completionHandler; ?? – StuartM Jul 03 '13 at 21:22
  • 1
    I added the header file declaration for you. You also may have some luck looking here: [Working WIth Blocks](http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html) It gives a nice overview on blocks and you can specifically look at the format you're looking for in the "A Block Should Always Be the Last Argument to a Method" section. – TheRobDay Jul 03 '13 at 21:59
  • Awesome! That was what I was originally looking for in the Apple docs and got lead to something less useful thanks again – StuartM Jul 03 '13 at 22:07
  • You can also check this answer about block declaration syntaxes on SO: http://stackoverflow.com/a/9201774/1226304 – derpoliuk Jul 04 '13 at 15:21
0

I am not quite sure, if I totally understand your question but maybe something like that would work:

+(void)createNewGameAgainst:(PFUser *)user2 callback:(void (^)(NSError *error))callback {
    // do something
    NSError *error = nil; // set to something useful if an error occurs
    callback(error);
}
zlajo
  • 2,173
  • 1
  • 19
  • 25