Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which one is better to use from NSInvocation or NSNotificationCentre or Delegate methods

Which one is better to use to flow the data from one class to another in the whole project?

NSInvocation

NSNotificationCentre

delegate methods

or by any other methods i am unaware of ??

like image 286
Pranjal Bikash Das Avatar asked Sep 04 '12 06:09

Pranjal Bikash Das


3 Answers

They all exist because they all serve different purposes. Briefly:

NSInvocation

Abstract message send to one object, with optional parameters, represented as an object. Not used very often, particularly since the introduction of blocks.

May also be used as a convenient way to avoid creating an NSOperation subclass (see NSIvocationOperation).

NSNotificationCenter

Broadcast a message to any number of unknown 'listeners'. One to many. Broadcaster need not know about listeners. Includes a user info dictionary for supplemental information. The most heavyweight/slowest of the lot -- not needed frequently, but seen often for convenience.

Delegates are sufficient substitutes in many cases.

delegate methods

Typically an abstract object which typically adopts a specific protocol. One to one relationship. Common means to handle an action rather than subclassing.


or by any other methods i am unaware of ??

Blocks (^) can also be used as callbacks/handlers and often as a more typesafe replacement for NSInvocations.

like image 162
justin Avatar answered Oct 21 '22 11:10

justin


Use a delegate if you want to talk to only one object. For example, a tableView has a delegate - only one object should be responsible for dealing with it.

Use notifications if you want to tell everyone that something has happened. For example in low memory situations a notification is sent telling your app that there has been a memory warning. Because lots of objects in your app might want to lower their memory usage it's a notification.

hope it helps.

like image 41
Anshuk Garg Avatar answered Oct 21 '22 10:10

Anshuk Garg


Just to add to everything everybody else has written, NSInvocation doesn't belong in this category, it just stores an invocation of a method with arguments and possible a target. Its used by NSNotificationCeter to do its work.

like image 36
Nathan Day Avatar answered Oct 21 '22 10:10

Nathan Day