Objective-C Blocks vs NSNotificationCenter for async completion
I’m working on a project that was created pre iOS 4.0, and is now only supporting iOS 4 and later. This means I am able to finally use Blocks. So what was being used to inform an object when a called async method passed or failed? NSNotificationCenter. This was the original code and it worked pretty well:
It allowed the method getSubscriptionFromServer to inform the caller in the Subscription class to know when getSubscriptionFromServer was complete, or failed. This might not have been the best way to handle this, but it doesn’t matter now.
Luckly I was able to make some changes, like dropping iOS 3.0 support and included the use of Blocks. This alowed me to change the above code to something like the following:
This helped clean up the code and encapsulate a lot of what was going on. The person using the Datacontroller class no longer needed to know about the Notifications. The method signature will now tell them all they need to know inorder to call getSubscriptionFromServer.
While Blocks is nothing new, I was happy to be able to finally use them to help clean up the codebase.