Reputation: 3159
This is a simplification, but I have an app that has the following methods:
//This is a common function that is used by various functions in my app to send a HTTP Post via NSURLConnection
- (void)sendPostWithURL:(NSString*)url
//This is the NSURLConnection function that has the response data
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
//myFunction1: send a HTTP Post and receive response1.json
-(void)myFunction1{
[self sendPostWithURL:@"http://www.somesite.com/response1.json"];
}
//myFunction2: send a HTTP Post and receive response2.json
-(void)myFunction2{
[self sendPostWithURL:@"http://www.somesite.com/response2.json"];
}
So as you can see, I have two functions (myFunction1 and myFunction2) that share a common NSURLConnection for sending and receiving data.
In the connectionDidFinishLoading() function, how can I tell whether the response is for myFunction1 or myFunction2?
I have one possibility, such as requesting that within the json response data, there is one key/value pair that allows me to see which response it is. However, I have a feeling that there is a more elegant way for me to do this within the code itself.
Any advice would be great! Thank you!
Upvotes: 0
Views: 353
Reputation: 12106
You are calling them "functions". If you start thinking of them as "methods" in a class, a lot of this will make more sense.
You need to encapsulate all of the NSURLConnection stuff in a class (called, say, Downloader.m). Downloader.m will have methods: sendPostWithUrl and connectionDidFinishLoading.
Then, when you need to download something, instantiate a new instance of downloader, and call it's sendPost method. You will have a specific class instance for each download operation and those should be easy enough to keep track of.
Upvotes: 2