I’ve got a simple login view that when you successfully log in, it goes and fetches some json from a server and inserts it into a database. So im using:
// NSURLRequest
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// appending data
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
// insert into db
}
I want a second connection now to download some documents, how can I utilise the methods above to pass it another URL and handle data in a different manner (not json) for the 2nd NSURLRequest in the same view?
Well, the first thing you need to do is set the
delegateof your newNSURLConnectionto this class again, so the methods get called (but you knew that). If you’re doing these one at a time, store a pointer to the activeNSURLRequest, something like:Then check the active request’s URL to differentiate the requests:
Note you can also compare the
NSURLobject directly with a stored version of it usingisEqual:(or the==operator).If you’re doing more than one at a time, you need another way to differentiate. I recommend using
ASIHTTPRequestorAFNetworkingto make this easier, but if you want to do it using Apple’s libraries then you’ll need to spin off multiple threads, keep a record of which thread number is handling which request URL, and use that information in yourconnectionDidFinishLoading:method.