从后台线程向服务器的异步请求

Dmy*_*tro 37 iphone objective-c nsurlconnection

当我尝试从后台线程向服务器发出异步请求时,我遇到了问题.我从来没有得到过这些要求的结果.显示问题的简单示例:

@protocol AsyncImgRequestDelegate
-(void) imageDownloadDidFinish:(UIImage*) img;
@end


@interface AsyncImgRequest : NSObject
{
 NSMutableData* receivedData;
 id<AsyncImgRequestDelegate> delegate;
}

@property (nonatomic,retain) id<AsyncImgRequestDelegate> delegate;

-(void) downloadImage:(NSString*) url ;

@end



@implementation AsyncImgRequest
-(void) downloadImage:(NSString*) url 
{  
 NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
             cachePolicy:NSURLRequestUseProtocolCachePolicy
            timeoutInterval:20.0];
 NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
 if (theConnection) {
  receivedData=[[NSMutableData data] retain];
 } else {
 }  

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
  [delegate imageDownloadDidFinish:[UIImage imageWithData:receivedData]];
  [connection release];
  [receivedData release];
}
@end
Run Code Online (Sandbox Code Playgroud)

然后我从主线程中调用它

asyncImgRequest = [[AsyncImgRequest alloc] init];
asyncImgRequest.delegate = self; 
[self performSelectorInBackground:@selector(downloadImage) withObject:nil];
Run Code Online (Sandbox Code Playgroud)

方法downloadImage如下:

-(void) downloadImage
{
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 [asyncImgRequest downloadImage:@"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/l/leopard-namibia-sw.jpg"];
 [pool release];
}
Run Code Online (Sandbox Code Playgroud)

问题是从不调用方法imageDownloadDidFinish.而且没有一种方法

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response
Run Code Online (Sandbox Code Playgroud)

叫做.但是,如果我更换

 [self performSelectorInBackground:@selector(downloadImage) withObject:nil]; 
Run Code Online (Sandbox Code Playgroud)

通过

 [self performSelector:@selector(downloadImage) withObject:nil]; 
Run Code Online (Sandbox Code Playgroud)

一切正常.我假设后台线程在异步请求完成它之前死亡,这导致了问题,但我不确定.我对这个假设是对的吗?有什么方法可以避免这个问题吗?

我知道我可以使用同步请求来避免这个问题,但这只是一个简单的例子,真实情况更复杂.

提前致谢.

nal*_*all 59

是的,线程正在退出.你可以通过添加:

-(void)threadDone:(NSNotification*)arg
{
    NSLog(@"Thread exiting");
}

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(threadDone:)
                                             name:NSThreadWillExitNotification
                                           object:nil];
Run Code Online (Sandbox Code Playgroud)

您可以使线程退出:

-(void) downloadImage
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    [self downloadImage:urlString];

    CFRunLoopRun(); // Avoid thread exiting
    [pool release];
}
Run Code Online (Sandbox Code Playgroud)

但是,这意味着线程永远不会退出.所以你需要在完成后停止它.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    CFRunLoopStop(CFRunLoopGetCurrent());
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    CFRunLoopStop(CFRunLoopGetCurrent());
}
Run Code Online (Sandbox Code Playgroud)

在" 线程指南"和" RunLoop参考"中了解有关"运行循环"的更多信息.

  • 对于使用ARC编译的项目,`NSAutoReleasePool`不可用,因此代码看起来像`@autoreleasepool {[self downloadImage:urlString]; CFRunLoopRun(); }` (6认同)