Reputation: 8099
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?
Upvotes: 0
Views: 45
Reputation: 21726
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
Upvotes: 2
Reputation: 119041
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).
Upvotes: 1