Are "relaying" delegate protocols bad practice in Objective-C?

For example, I have three custom object in the C-of Objective: ClassA: UIViewController <ClassBDelegate>

, ClassB: NSObject <ClassCDelegate>

, ClassC: NSObject

. ClassA is a subclass of UIViewController that displays an activity indicator until a certain network event occurs in ClassC. ClassB has ClassC, among other classes, and is responsible for passing events from ClassC to ClassA. ClassA has ClassB and ClassB has ClassC, there is no direct connection from ClassA to ClassC.

Now let's say as soon as a certain network event happens in ClassC, ClassC will disconnect - (void)someAction;

. ClassB conforms to the ClassC delegate protocol and its method is - (void)classC:(ClassC *)classC didPerformSomeAction;

called. In turn, ClassB fires -(void)thisOtherAction;

, and since ClassA conforms to the ClassB delegate protocol, it is triggered by a call -(void)classB:(ClassB *)classB didPerformThisOtherAction;

that efficiently retrieves the network event from ClassC.

My question is, is this bad practice, and if so, is there an easier way to bridge the gap between ClassA and ClassC? I hesitate to use NSNotification because I find delegate protocols to be more elegant. However, it looks like a baton to me. I would appreciate any information on popular conventions in a scenario like this.

+3


source to share


2 answers


Do not think of a delegate as more elegant than notifications, just consider that they apply to different situations:

Delegates - used when one element is interested in a result or provides a service to one other element to which it is related



Notifications - for use when (potentially) several different elements are interested in one (or more) events that are posted by other elements to which they may or may not be related

It's not 100% difficult or fast. You can create a delegate that can call back many observers. You can also pass a block between classes so that another object takes responsibility for the delegate callback. But your situation seems to be more appropriate for notification handling.

+1


source


It seems that you are describing a mediator pattern or an adapter pattern . It is also possible that you have an average person code smell . It really depends on what you want to achieve.



+1


source







All Articles