0

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?

fes
  • 2,465
  • 11
  • 40
  • 56

1 Answers1

1

Well, the first thing you need to do is set the delegate of your new NSURLConnection to 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 active NSURLRequest, something like:

@property (nonatomic, assign) NSURLRequest *activeURLRequest;

Then check the active request's URL to differentiate the requests:

- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *urlPath = [[activeURLRequest URL] path];
    if([urlPath isEqualToString:@"http://my.url.string/1"])
        // insert into DB
    else if([urlPath isEqualToString:@"http://my.url.string/2"])
        // do the other thing
}

Note you can also compare the NSURL object directly with a stored version of it using isEqual: (or the == operator).

If you're doing more than one at a time, you need another way to differentiate. I recommend using ASIHTTPRequest or AFNetworking to 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 your connectionDidFinishLoading: method.

  • I see what you mean, I'll play with this active URL idea and process information according the URL. I didn't want to use ASIHTTPRequest (which is great) but I think it's not being supported anymore? i.e. no IOS5 updates for example. Cheers. – fes Dec 01 '11 at 23:11
  • I agree `ASIHTTPRequest` isn't being supported anymore, but that doesn't mean it's broken. It still works great for me, there's a lot of finnicky thread-safe coding in there which has been refined over the years. It's also the simplest way to cache particular requests on disk in a custom location (that I've seen) and it handles HTTP redirects very well. –  Dec 01 '11 at 23:17
  • do you have any issues with ASIHTTPRequest for iOS5? I remember doing a build and it was throwing ghastly warnings of which I had no idea about. – fes Dec 01 '11 at 23:22
  • I'm using it in an `iOS5`-compatible app which is in the App Store right now. It's used for both retrieving (and caching) XML requests, and as a download manager/storage framework. No issues. Of course, I'm not using `ARC`; it'd need to be reworked a bit if you enabled that. –  Dec 01 '11 at 23:28
  • I will need to look at it again. I'm not sure what ARC is so I'll have to check that. I just need it to be able to download json and files asynchronously and so Apple's libraries may get to complex if ASIHTTPRequest can do it for me. – fes Dec 01 '11 at 23:32
  • ARC is mostly explained [here](http://stackoverflow.com/questions/6385212/how-does-the-new-automatic-reference-counting-mechanism-work). If you don't need caching, I'd say go with `AFNetworking` instead. It already contains a `JSON` component. Link in the answer. –  Dec 01 '11 at 23:34