gpt4 book ai didi

objective-c - 从后台线程到服务器的异步请求

转载 作者:IT老高 更新时间:2023-10-28 11:48:07 27 4
gpt4 key购买 nike

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

@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

然后我从主线程调用它

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

方法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];
}

问题是永远不会调用 imageDownloadDidFinish 方法。而且没有任何方法

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response

被调用。但是,如果我更换

 [self performSelectorInBackground:@selector(downloadImage) withObject:nil]; 

 [self performSelector:@selector(downloadImage) withObject:nil]; 

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

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

提前致谢。

最佳答案

是的,线程正在退出。您可以通过添加以下内容来查看:

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

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(threadDone:)
name:NSThreadWillExitNotification
object:nil];

您可以通过以下方式阻止线程退出:

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

CFRunLoopRun(); // Avoid thread exiting
[pool release];
}

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

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

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
CFRunLoopStop(CFRunLoopGetCurrent());
}

Threading Guide 中了解有关运行循环的更多信息和 RunLoop Reference .

关于objective-c - 从后台线程到服务器的异步请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1728631/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com