views:

103

answers:

2

Hello community!

I am a as3 developer, I am used to use handlers everytime I launch an event and I am wondering which is the best way / practice to do that in Objective C.

Let's say I want to call a diferent services from my backend.

In as3 would be something like this to listent to the finish event:

service.addEventListener( Event.COMPLETE, handler_serviceDidFinished ) service2.addEventListener( Event.COMPLETE, handler_serviceDidFinished2 )

But how can I do the same in Objective C?

The problem is I already created the protocols and delegates, but how can I separate each response from the server?

For example:

-(void)callService:( NSString * )methodName withParameters:(NSMutableDictionary *) parameters
{
...
    if (self.delegate != NULL && [self.delegate respondsToSelector:@selector(serviceDidFinishSuccessfully:)]) {
        [delegate serviceDidFinishSuccessfully:data];
    }
}

Well I'm trying to create a generic delegate here, so how can I separate each response for every service?

My first idea is that maybe I should return the name of the service method in the delegate call to identify each service. Maybe I should create a UID for each service and pass it the same way...

Any idea?

+2  A: 

You might be interested in Objective-C notifications. Basically, these allow you to use a "notification center" to which you "post" different messages; then, all the callback/delegate classes can "register" for certain notifications, so that they know when to do things. This way, all your backend has to do is post the notification, and not worry about whether certain delegates respond to selectors, etc. See Cocoa Core Competencies: Notifications (as well as the NSNotificationCenter and NSNotification class references) for more.

Tim
A: 

Look at the NSURLConnection delegate - the common pattern is to pass back the object calling you as the first parameter, exactly so you can distinguish among several objects you are a delegate for.

Notifications are a useful thing to indicate task completion, but not really good for handlers that need to affect the processing flow of the object.

Kendall Helmstetter Gelner