NSNotificationCenter

is awesome (in moderation).


NSNotificationCenter objects broadcast information across your program, so you can trigger certain code in one class based on what happens in another.

Having just learned this framework, I was beyond thrilled to have found the perfect opportunity to implement it in our Instagram clone.

After a user uploads a photo, they’re brought back to the main photo feed. But when they arrive at the feed, their photo is still being uploaded in the background. This requires the user to manually refresh the feed to see their photo included in it.

Saving a file to the Parse cloud asynchronously

The chunk of code you see above (from our EditPhotoViewController.m) does not execute until the file is uploaded to Parse. Or more importantly, not soon enough for the FeedViewController.m to retrieve the data and display it to the user.

If only there was a way to notify the FeedViewController that the EditViewController’s saveInBackgroundWithBlock: finished executing..

Step 1: Make the class that is to receive the notification an observer during the nib loading.

Step 2: Write the method that the receiver sends to the notificationObserver to notify it of the notification posting.

Step 3: Create a notification with a given name & sender and posts it to the receiver.

Now, when the user’s photo is done uploading they will see it appear in the feed without having to refresh manually.

Disclaimer: Yes, NSNotificationCenter can be awesome in low-thread environments, but I’ve been cautioned multi-threading can get messy so use wisely.