views:

157

answers:

2

I have a "BSjax" class that I wrote that lets me make async calls to our server to get json result sets, etc using the ASIHTTPRequest class. I set it up so that the BSjax class parses my server's json response, then passes control back to the calling view controller via this call:

[[self delegate] performSelectorOnMainThread:@selector(bsRequestFinished:) withObject:self waitUntilDone:YES];

... where "bsRequestFinished" is the callback method in the calling view controller. This all worked fine and well until I realized that some pages are going to need to make different types of requests... i.e. I'll want to do different types of things in that callback function depending on which type of request was made.

To me it seems like being able to pass different callback function names to my BSjax class would be the cleanest fix... but I'm having trouble (and am not even sure if it's possible) to pass in a variable that holds the callback function name and then replace the call above with something like this:

[[self delegate] performSelectorOnMainThread:@selector(self.variableCallbackFunctionName) withObject:self waitUntilDone:YES];

... where "self.variableCallbackFunctionName" is set by the calling view controller when it calls BSjax to make a new request.

Is this even possible? If so, advisable? If not, alternatives?

EDIT: Note that whatever fix I arrive at will need to take into account the reality that this class is making async requests... so I need to make sure that the callback function processing is correctly tied to the specific requests... as I can't rely on FIFO processing sequence.

EDIT 2: Looking like passing in a SEL type variable is the way to go? What I'm fighting with now is figuring out the syntax for saving it as a retained property of the BSjax class.

+1  A: 

What type of variable is variBkecallbackfunctionname? An NSString? If it is, you'll probably want something like this:

[[self delegate] performSelectorOnMainThread:NSSelectorFromString(self.variableCallbackFunctionName) withObject:self waitUntilDone:YES];

I am at an iPad so I can't test this, but give it a try.

Edit: and if variableCallbackFunctionName isn't an nsstring, the easiest way might be to make it, if I understand your question.

Edit 2: docs for that are at http://developer.apple.com/mac/library/iPad/index.html#documentation/cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html%23//apple_ref/c/func/NSSelectorFromString

Tom H
nice... u the man! didn't even know about that NSSelectorFromString function. worked perfectly!
codemonkey
+2  A: 

You can pass around selectors as SEL values. For example:

SEL oneSelector = @selector(bsRequestFinished:);
SEL anotherselector = @selector(somethingElse:);
NSString *nameOfSelector = askUserForSelectorName();
SEL dynamicSelector = NSSelectorFromString(nameOfSelector);

So just make your variableCallbackFunctionName a SEL and it's even simpler than you were hoping for:

[[self delegate] performSelectorOnMainThread:self.variableCallbackFunctionName withObject:self waitUntilDone:YES];
Chuck