NSURLConnection and connectionDidFinishLoading sharing between multiple functions

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
 [self sendPostWithURL:@"http://www.somesite.com/response1.json"];

//myFunction2: send a HTTP Post and receive response2.json
 [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!


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.

Need Your Help

Windows authentication using phantomjs

phantomjs windows-authentication

I'm looking into phantomjs as a possible solution for UI automation in my latest project, but I can't seem to get windows authentication to work properly. I've tried setting the page.settings.userN...

How do I make a web server accessible though the internet?

jquery node.js web web-deployment

I have created a basic web server using node.js which listens to port 3000 on my desktop. It serves static HTML files (jQuery mobile documents actually).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.