0

I've had a look at lots of different answers about blocks and I'm still struggling to figure out how to use it.

Here is what I have so far...

@interface myController ()
   typedef void (^CompletionBlock)();
@end

Then I have declared a method like this:

-(void)reloadDataWithCompletions:(CompletionBlock)completionBlock{
    [self.tableView reloadData];
    completionBlock();
}

What I don't know how to do is how to write the code in the completionBlock. Do I write another method called completionBlock ? Like this

-(void) completionBlock{
    // do something here once the first method is finished?
 }
Linda Keating
  • 2,215
  • 7
  • 31
  • 63

1 Answers1

2

You need to call the reloadDataWithCompletions: method passing in the block you want executed. Something like this:

[self reloadDataWithCompletions:^{
    // The code you want executed in the block
}];

BTW - there is no need for a block in this case. A completion block is only really needed when dealing with asynchronous calls made on other threads. Since your reloadDataWithCompletions: method only calls reloadData on a table view, and since that method is synchronous, the use of a block is pointless.

You would get the same result in this case by doing:

-(void)reloadData {
    [self.tableView reloadData];
}

and calling it like:

[self reloadData];
// code you want executed after the reload is done
rmaddy
  • 314,917
  • 42
  • 532
  • 579