0
votes

I was using NSURLConnection in a synchronous way before running on a background selector, so when I moved over to ASIHTTPRequest I did the same with this framework.

So, is it a bad idea to do something like the following?

// From another method
[self performSelectorInBackground:@selector(callDatasource) withObject:nil];


- (NSData *)callDatasource {

    NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:someURLthatIamusing];
    [request setTimeOutSeconds:50.0];
    [request startSynchronous];
    NSError *error = [request error];
    NSData *returnedData;
    if (!error) {
        returnedData = [request responseData];
    } else {
        // do something with error

    }

    [self performSelectorOnMainThread:@selector(done) withObject:nil waitUntilDone:NO];

    [apool release];

    return returnedData;

}//end

What would be the advantage to use the ASIHTTPRequest and asynchronous methods along with the delegate methods?

2

2 Answers

2
votes

From experience, sometimes odd things can happen when using ASIHTTPRequest synchronous requests off a secondary thread: the download activity icon in the status bar not disappearing upon download completion is one issue I've noticed from time to time. I've had no major problems in the past, but I use the asynchronous methods now rather than your approach. The ASI asynchronous methods are by the nature of being a widely used library more highly tested than my own implementation could ever be.

There are a number of advantages with using the asynchronous methods - you mention the delegate methods, but the latest release of ASI actually also supports blocks, which is a great leap forward (dealing with multiple synchronous calls used to be a bit of a pain due to the shared delegate methods (or unique delegates for each asynchronous call). But with blocks you can now get rid of the delegates entirely. I've found them to be really useful. Plus if you use multiple contributors it can make readability a lot easier.

0
votes

Also, by doing it Async, you can more easily track progress through the setProgressDelegate command.