Question

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 ??

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

I use delegation. It's portable across platforms and tighter (not all going through a central dispatching system).

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