0

我采用了一个 iOS 应用程序并且遇到了异步请求问题。

我们有一个具有以下代码的 WebService 类;

// 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 {

这在后台线程中看起来很普通 NSURLRequest 。但是,一旦调用结束,它就会冻结 UITableViewController。也许我误解了应该发生的事情,但它似乎应该能够滚动表格视图。上面的代码有什么问题吗?我正在调试的一种可能性是我正在使用 SDWebImage 来降低缩略图,并且冻结可能是由于下载了所述图像,但我认为这将发生在后台线程中。上面的 NSURLRequest 是否应该阻塞主线程,有什么方法可以验证我可以验证 SDWebImage 是这里的罪魁祸首吗?

谢谢

4

1 回答 1

0

典型的模式是创建请求。将 self 设置为委托并处理委托方法中的响应,如下所示:

-(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.

于 2012-07-01T23:24:39.697 回答