views:

544

answers:

1

The following concerns an iPhone app.

I want to use a singleton to handle an asynchronous URL request. I've got that covered but what is the best coding practice way to tell the singleton where to send the response when it is received? I need the response to be returned to the class which originally called the singleton.

Could I pass a pointer to the calling class to the singleton, along with the delegate method (of the calling class) to call when the response is received?

Thanks

+2  A: 

First of all, singletons are usually a bad design and this looks exactly like a situation where you should not need a singleton. See Singletons are Pathological Liars by Miško Hevery and other articles on his blog. (I hope this does not look arrogant, getting rid of the singleton will probably make your design better and coding easier.)

Second, if I understand your question correctly, you could pass the singleton a selector that should be called after the singleton receives the data. The API in the singleton class could look like this:

- (void) download: (NSURLRequest*) request andTell: (id) delegate to: (SEL) doThis;

Then after you finish loading the data you would do:

[delegate performSelector:doThis withObject:receivedData];

Does this answer the question?

zoul
Exactly the sort of answer I was looking for, thank you.
FRS