0

I have adopted an iOS app and am having problem asynchronous requests.

We have a WebService class that has the following code;

// create the request
NSURLRequest* request=[NSURLRequest requestWithURL:url
                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                       timeoutInterval:30.0];

self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (self.connection) {
  self.receivedData = [NSMutableData data];
} else {

This seems like pretty vanilla NSURLRequest in a background thread. However, once this call goes out, it freezes up the UITableViewController. Maybe I'm misunderstanding what is supposed to happen but it seems like it should be able to scroll the table view. Is there anything wrong with the above code? One possibility that I'm debugging is that I'm using SDWebImage to downlowd thumbnails and the freezing might be due to downloading of said images but I would think this would take place in a background thread. Should the above NSURLRequest block the main thread and is there any way I can verify that I can verify that SDWebImage is the culprit here?

thx

timpone
  • 19,235
  • 36
  • 121
  • 211
  • You have not posted enough code to understand what is actually going on. Your talking about scrolling a UITableViewController where is that? – Cliff Ribaudo Jul 01 '12 at 23:03
  • The UITableViewController is just a vanilla UITableViewController - spans probably like 60 lines though. The question is more about whether the above code should be blocking the main thread under any circumstances – timpone Jul 01 '12 at 23:07
  • 1
    You could use the debugger to pause the program when the UI freezes and look at the list of threads to see what is running on the various ones. – Phillip Mills Jul 01 '12 at 23:07

1 Answers1

0

Typical pattern for this is create the request. Set self to delegate and deal with response in delegate methods like so:

-(void)someMethod
{
     NSURLRequest* request=[NSURLRequest requestWithURL:url
         cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
     self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
     if(statusCode == 200) // or whatever makes sense in your situation.
         self.receivedData = [NSMutableData data];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [requestData appendData:data];
}

Or some such.... but then Im not sure where your calling all this stuff from... and you appear to be doing BOTH in the same method, which may or may not be connection:didReceiveResponse.

Cliff Ribaudo
  • 8,932
  • 2
  • 55
  • 78
  • yes, so this would be done in a non-blocking background thread, right? I've had two other things going on this afternoon – timpone Jul 02 '12 at 00:36
  • The NSURLConnection IS asynchronous, the delegate method [connection:didRecieveData] gets called with the data when and as it comes in. You can do this in kind of any controller you want as long as you don't block anything waiting for data. Just go about your business until the connection tells you its done. Than handle the data. Read the NSURLConnectionDelegate docs, that will help a lot. – Cliff Ribaudo Jul 02 '12 at 10:46