I am familiar with the delegation pattern and populating my delegates, especially when making asynchronous calls that still occur when my view controllers disappear. I passed the delegate and the callback successfully returns the nil object.
Now I'm experimenting using completion blocks to make my code a little easier to read.
I call the network service from my view controller and pass in a block that updates my UITableView. Under normal conditions, it works fine. However, if I leave the view before its completion, the completion handler block is executed, but the UITableView is now a zombie.
What is the usual pattern for this?
UPDATE WITH EXAMPLE CODE
This application is for iPad, I simultaneously have two view controllers, for example, with a split view. One of them is a detail, and the other is a grid of images. I click on the image and it reports the details to download the information. However, if I click on the images too quickly before they have a chance to make a network call - I have problems. When changing images, the code below is called, which counts the selected images ....
So, here is my dilemma, if I use the code below - it works fine, but it flows in the tools if you switch images before the network responds.
If I remove __block and go into myself, it will work with zombies.
I canโt win ... Iโm sure that I am missing something fundamental in using blocks.
__block UITableView *theTable = [self.table retain]; __block IndexedDictionary *tableData = [self.descriptionKeyValues retain]; FavouritesController *favourites = [Container controllerWithClass:FavouritesController.class]; [favourites countFavouritesForPhoto:self.photo completion:^(int favesCount) { [tableData insertObject:[NSString stringWithFormat:@"%i", favesCount] forKey:@"Favourites:" atIndex:1]; [theTable reloadData]; [tableData release]; [theTable release]; }];
Any tips? Thanks
SECOND UPDATE
I changed the way you download favorites. Instead of being single favorites, I create a copy every time I change the photo. Replacing this and killing the old one โ the block has nowhere to call back (I think it doesn't even exist), and now my code looks like the one shown below, and it seems to work:
[self.favourites countFavouritesForPhoto:self.photo completion:^(int favesCount) { [self.descriptionKeyValues insertObject:[NSString stringWithFormat:@"%i", favesCount] forKey:@"Favourites:" atIndex:1]; [self.table reloadData]; }];
It does not flow and does not seem to crash either.