views:

585

answers:

3

Hey guys,

Just to give background for my situation, I have a manager singleton that pulls data from a webserver and provides access to the downloaded data. I have several types of views that will consume this data, but only one view at any time will need to receive events.

I was just wondering what people prefer to use when they need to get events from a singleton. Do you use NSNotificationCenter, Target/Action, or delegate?

Thanks for any help.

+1  A: 

If there are going to be a large number of events, then you want to stay away from NSNotifications.

For the least amount of overhead I would go with the delegate pattern, although I don't think that target/action has much more overhead than delegates.

Try your favorite way and if there is a problem profile or try a different approach.

I usually start with the easiest to get implemented. For example I once tried to use notifications for some interface code I had written years ago but with 30-60 updates/second the whole interface bogged down unacceptably so I went with delegates which fixed the problem.

Mark Thalman
+3  A: 

Are you really, really sure only one view needs to receive events? For instance, you don't have a master view that would need access to the same update that a subview was notified about?

If you truly have only one view controller needing updates at a time ever, I might use a delegate approach. Here's something to consider - what happens if you are in the middle of receiving an update and the user change screens... is that OK? would you cancel the request?

Anything more than one, or if that in-flight changing delegate scenario has issues, then you may well be better off with a notification that anyone can hook into. It's best to keep the notification light with some kind of reference to the change and have the receiver have to look up the altered data.

Kendall Helmstetter Gelner
Thanks, I didn't even think of what would happen if they switched screens mid call. Thanks to Mark too for his answer.
Meroon
A: 

I made a reusable singleton class from which you can inherit to get singleton properties. You can see source code here: http://www.devbypractice.com/reusable-singleton-class-in-objective-c-for-iphone-and-ipad/

jey350