Callback

Completion Callback (Closure)

Callbacks are functions that often take the form of a closure. We do a lot of asynchronous work on mobile devices in an effort to keep our code from blocking the main thread.

But they are very usable for making code more clear and readable. And as we all know clean readable short code is a code that is easier to debug and contains fewer bugs.

Delegation

Delegation is a design pattern that enables a class or structure to hand off (or delegate) some of its responsibilities to an instance of another type

Advantages of using delegate

  • Loose coupling. Indeed, with delegate pattern, we have interacting parties decoupled from each other without exposure of who exactly they are.
  • Unlike many other callback techniques, with the delegate, you can not only notify but also ask for data. In Cocoa, you can often see DataSource protocols, which stand exactly for this purpose.
  • Speed. Since calling a method on a delegate is nothing more than direct call of a function, with delegate you can achieve absolutely best performance among other callback techniques, which have to spend time on more sophisticated delivery of the call.

Disadvantages of using delegate

  • Single recipient, one to one relationship
  • Cumbersome implementation.
    • In order to use delegate we need to do quite a few steps:
      • define a new protocol
      • define a weak property for the delegate
      • implement the protocol in the target type
      • assign the delegate reference.
      • [Obj-C] check if the delegate can handle the message with respondsToSelector:

Notification

  • Notifications are based on a subscribe-and-publish model that allows an object (the publisher) to send messages to other objects (subscribers/listeners).
  • The publisher never needs to know anything about the subscribers.

Advantages of using NotificationCenter

  • Multiple recipients. NotificationCenter transparently delivers a Notification to all subscribers, could there be one, or thousand, or none - this class will take care of the delivery for you.
  • What’s also notable - the sender cannot know how many subscribers he has. Not to say it’s good or bad, it’s just how this tool is designed.
  • Loose coupling. When using NotificationCenter the only thing that couples sender and receivers together is the name of the notification they use for their communication. If you include a payload to the notification, both parties would need to have symmetric boxing and unboxing of the data.
  • Global access point. When you don’t need (or just don’t care about) the dependency injections in your code, the singleton-style method defaultCenter allows you to connect two random objects together with ease.

Disadvantages of using NotificationCenter

  • Global access point. Yes, this is also a disadvantage. Any globally accessible stuff breaks the testability of your code and even singleton as a design pattern many people consider as an anty-pattern.
  • Non-obvious control flow. If you are trying to understand the business logic of the program and see a place where a Notification is sent - the only way to continue exploration is by finding all the recipients manually with a text search in the entire project - because they can be anywhere!
  • Transferring data is very error prone because of boxing and unboxing with a Dictionary (NSDictionary). Even when used from type-safe Swift, the compiler cannot check the types as well as the structure of the Dictionary.
  • There is no control over who is eligible of sending the particular notification

Target Action

Controls communicate directly with their associated target object using action messages. When the user interacts with a control, the control calls the action method of its target object—in other words, it sends an action message to its target object. Action messages are not events, but they may still take advantage of the responder chain. When the target object of a control is nil, UIKit starts from the target object and walks the responder chain until it finds an object that implements the appropriate action method

Key Value Observing

With KVO observers can be notified of any changes of a @property values.

results matching ""

    No results matching ""