Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Delegates in iOS

Tags:

I am making an object that goes to download stuff for all of my view controllers. The object is singleton instance and has a callback method with received data once the download is completed. It also has a delegate property so that it knows which object to call back to after the download is done.

There are multiple controllers that use this shared instance, and my question is how to call back to the correct view controller that requested the download.

My approach is to use delegation, but the problem is that since other view controllers are also its delegate, the download object could call back to every object and this will be hard to track.

like image 945
Chris Lin Avatar asked Oct 08 '12 03:10

Chris Lin


People also ask

What are iOS delegates?

What is a delegate? Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. A delegate allows one object to send messages to another object when an event happens.

What is difference between delegate and datasource in iOS?

A data source is almost identical to a delegate. The difference is in the relationship with the delegating object. Instead of being delegated control of the user interface, a data source is delegated control of data.

What is multicast delegate Swift?

Save. Multicast Delegate is a generic wrapper around yet another delegate protocol, that means it create one-to-many delegate relationships. This allows providing an object with an array of delegates.

What is Delegate design pattern in iOS?

One of the most important design patterns on iOS is the delegate pattern. It is the design pattern most commonly used in many Apple frameworks and libraries. Being familiar with this pattern will: Add a tool to solve reoccurring software problems in your code. Quickly navigate and use Apple APIs.


2 Answers

I've worked on projects where people have attempted to use multiple delegates and it's basically a bad idea. The delegate pattern is about a 1 to 1 relationship between a class and it's delegate. Whilst it is possible to achieve some level of multiple delegation through switching the delegates in and out, it's more likely to lead to unpredictable behaviour and bugs.

My recommendation would be to change how you are thinking about this. You have two options as I see it:

  1. Switch to an Observer pattern where you can register multiple observers which your main class can interact with. This is useful where your observers all implement the same protocol and where your main class wants to be aware of the observers and interaction with them.

  2. Broadcast NSNotifications to indicate state changes and events. Here is a more decoupled approach because the main class does not need to know who is listening and does not directly interact with them. Other can start and stop being notified at their leisure. It also has the advantage that you do not need to create or implement a separate protocol. Instead you register the classes that need to know about changes with the NSNotificationCenter which in turns handles all the routing of notifications for you.

like image 136
drekka Avatar answered Sep 28 '22 11:09

drekka


It actually sounds like the delegate pattern might not be the best approach here.

I would look into NSNotificationCenter instead.

The basic idea is that your singleton doing the net connection posts a notification (with something like postNotificationName:object:userInfo:) , saying that new data is available. Within this notification, you can pass a dictionary object (userInfo) that holds the data you've fetched, or info on what parts of your Model contain updated data.

Then, your other view controllers can register themselves to 'observe' these notifications by calling addObserver:selector:name:object:. Generally speaking, when a vc becomes visible I call addObserver, and removeObserver when it's being hidden or transitioned out.

Good luck!

like image 23
Chazbot Avatar answered Sep 28 '22 10:09

Chazbot