Multiple Delegates in iOS
Asked Answered
T

6

22

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.

Tanberg answered 8/10, 2012 at 3:4 Comment(2)
Could a request to start downloading include a pointer to the receiver of the data? Then you could just call the delegate method on the receiver of each request.Opia
I ran into similar problem and have found ready solution github.com/aleph7/MultiDelegateNunciata
B
52

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.

Briquet answered 8/10, 2012 at 3:15 Comment(2)
Thanks for explaining. NSNotification worked out beautifully for me.Salpingectomy
a bit late, but for those with similiar problem: check out the publisher/subscriber patternLuke
L
14

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!

Labyrinth answered 8/10, 2012 at 3:17 Comment(1)
There are cases where multiple delegates are useful. I f you want to keep a high framerate in rendering output with multiple views or data transfer, notifications are slower than direct delegates methods.Francois
P
0

Delegation doesn't seem like the right solution to this problem. How about requiring the requesting view controller to provide an object (its self) and a selector for you to call as a completion notification? Of course, you'll need a place to store that object and selector until the download completes. Hopefully you have (or could create) an object for this.

Photoelasticity answered 8/10, 2012 at 3:46 Comment(1)
You could also use success blocks.Wakefield
A
0

i recommend to use one of these ways

observer:

when use data that you want to inform other object are near to primitive ones.for example when you are using 'NSMutableArray' you can not inform the change in one of object by the standard implemented pattern at least you need to implement one for your self that is not reusable that much

Notification

when your interaction with destination object (those need to be inform) is in one-way.it means you don't need any acknowledge or other data back from them.

delegate

when there is one object to inform at each time step.

note:block use for success and fail is not a pattern to broadcast data its about to queue task when you don't know when they are finishing or failing like network operations

EDIT: how to create notification | multi delegate issues and implementation

Ardor answered 6/12, 2015 at 5:55 Comment(0)
P
0

While I agree with most of the answers here, if you did actually want to achieve multiple delegates you could potentially declare an array of delegates and send messages to all delegates within that array. If your protocol has optional delegate methods you safely check using responds(to aSelector: Selector!) -> Bool before invoking (being mindful of memory management, as those delegates would be strongly referenced in the array). Again I do agree that multiple delegates is likely a bad architectural idea and using blocks or notification center would suit your needs better.

Polyphonic answered 25/2, 2018 at 19:39 Comment(0)
A
0

One approach, which works for me if you only have one other object to forward messages to is to create a forwardingDelegate This does not end up with issues of hard to debug ordering of delegates and it does not unnecessarily create a dependency on the other object. Keep in mind, if you have many objects then this might not be the best approach, it is mainly for one additional object but this could be extended to support an array of objects so long as there is one that receives the SDK and forwards it to the other objects [1]. Note that every method that is needed for the forwarded object needs to pass it along, even if it is not used by the forwarding object.

For example, if I need to forward the messages coming from the mapView delegate:

 - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated 
{
    // handle this object here. 

    if ([self.forwardingDelegate respondsToSelector:@selector(mapView:regionDidChangeAnimated:)])
    {
         [self.forwardingDelegate mapView:mapView regionDidChangeAnimated:animated];
    }

    // or handle this object here. 
}

[self.forwardingDelegate mapView:mapView regionDidChangeAnimated:animated];

The forwarding property would be declared like this.

@property (nonatomic) id<MKMapViewDelegate> forwardingDelegate;

And the other object would adopt the protocol as if it were receiving the original message.

[1] The array approach for multiple delegates may get tricky because then you don't have as much control over what order the delegates get called, as was mentioned in other posts.

Almund answered 12/12, 2019 at 6:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.