Which is better to use from the methods NSInvocation or NSNotificationCentre or Delegate

Which one is better to use to transfer data from one class to another throughout the project?

NSInvocation

NSNotificationCentre

delegate methods

or any other methods that I donโ€™t know about

+6
source share
4 answers

They all exist because they all serve different purposes. In short:

NSInvocation

An abstract message is sent to one object with optional parameters represented as an object. It is not used very often, especially since the introduction of blocks.

It can also be used as a convenient way to avoid subclassing NSOperation (see NSIvocationOperation ).

NSNotificationCenter

Send a message to any number of unknown "listeners". One to many. The compiler should not know about the listeners. Includes a user information dictionary for additional information. The heaviest / slowest of the lot - often not required, but often visible for convenience.

In most cases, delegates are sufficient substitutes.

delegation methods

This is usually an abstract object that usually accepts a specific protocol. One to one relationship. A common tool for handling an action, not a subclass.


or any other methods that I donโ€™t know about

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

+7
source

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

Use notifications if you want to tell everyone that something happened. For example, in low memory situations, a notification is sent informing your application that there is a memory warning. Since many objects in your application can reduce memory usage, this is a notification.

hope this helps.

+1
source

Just to add to everything that everyone else wrote, NSInvocation is not in this category, it just stores a method call with arguments and a possible target. It is used by NSNotificationCeter to do its job.

+1
source

I use delegation. It is carried on different platforms and is denser (not everyone passes through the central dispatch system).

0
source

Source: https://habr.com/ru/post/924494/


All Articles