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

4

6

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

Melbamelborn answered 4/9, 2012 at 6:0 Comment(2)
@Veelian -- but it has restrictions also right??Melbamelborn
In the same family as the notification center, Key Value Observing can be very useful in some cases, if you want one or several objects to react automatically to another object's properties change.Exterminatory
C
7

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.

Closegrained answered 4/9, 2012 at 6:8 Comment(0)
S
1

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.

Silkworm answered 4/9, 2012 at 6:7 Comment(2)
got it..but what about NSInvocation??Melbamelborn
NSInvocation comes in handy when you want to send a message to an object at a different point in time, or send the same message several times. NSInvocation allows you to describe the message you are going to send, and then invoke it (actually send it to the target object) later on.Silkworm
M
1

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.

Malapropism answered 4/9, 2012 at 6:16 Comment(2)
so its a specific way of how NSNotificationCentre behaves..right??Melbamelborn
NSInvocation don't exist for NSNotificationCenter, they can be used in lots of places. They are also used in interprocess communication, they are probable used by NSTimers, an NSInvocation is just a way to represent a method invocation with its method selector and arguments. NSNotificationCenter probable uses NSInvocation internally, but who knows.Malapropism
R
0

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

Rigger answered 4/9, 2012 at 6:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.