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.

2
  • 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. Commented Oct 8, 2012 at 3:13
  • I ran into similar problem and have found ready solution github.com/aleph7/MultiDelegate Commented May 11, 2013 at 14:37

6 Answers 6

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.

2
  • 7
    Thanks for explaining. NSNotification worked out beautifully for me.
    – abc123
    Commented Nov 17, 2013 at 19:47
  • a bit late, but for those with similiar problem: check out the publisher/subscriber pattern
    – salocinx
    Commented Jun 19, 2015 at 20:50
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!

1
  • 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. Commented Sep 25, 2018 at 20:15
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.

1
  • 1
    You could also use success blocks.
    – fabb
    Commented Mar 21, 2014 at 7:45
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

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.

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.