Question

Is it best practice to communicate an event or something similar (like successful login) through NSNotificationCenter or is there any other controlling mechanism you can recommend?

Was it helpful?

Solution

As for me I do not like to use NSNotificationCenter because it is overloaded with a big amount of system and custom events.

If you add a lot of observers to NSNotificationCenter, you should not forget to remove these observers, also sometimes it is difficult to know the sequence, in which observing methods will be called. Also NSNotificationCenter doesn't check or manage adding the same observer more than one time(it sometimes becomes a real trouble, when you addObserver not in the correct place).

So: Why just not create some LoginManager singleton which will contain all the needed data and manage all login behaviour? It will contain some data as : isAuthorithed, etc.. And of course if you need to implement Observer pattern, your singleton class can implement this in the same way as NSNotificationCenter

OTHER TIPS

That depends on what the event is and what classes are likely to need to know about it. For login / logout notifications are a good option because many different classes may want to respond tithe event. That doesn't mean that you can't also have a delegate / block callback for use by the class which triggered the login.

Generally, notifications for general things that could be interesting to many classes and direct callbacks for specific events (and the instance which triggered the event).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top