NSURLConnect只工作一次

时间:2012-11-08 10:42:53

标签: objective-c xcode

我正在尝试做一个请求,但它只在第一次工作....

这是我的代码:

NSArray *infos = [rows objectAtIndex:indexPath.row];
NSString *thumbPath = thePath;

NSURLRequest *thumbRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:thumbPath]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];

self->thumbConnection = [[NSURLConnection alloc] initWithRequest:thumbRequest
                                                        delegate:self
                                                startImmediately:YES];

     self->thumbData = [[NSMutableData alloc]init];

收到回复时

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

if (self->thumbData == nil) {
    self->thumbData = [[NSMutableData alloc]init];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

self.thumbImage.image = [UIImage imageWithData:self->thumbData];

self.thumbActivityView.hidden = YES;

self->thumbData = nil;
}

当didFinishDownloading:    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {

self.thumbImage.image = [UIImage imageWithData:self->thumbData];

self.thumbActivityView.hidden = YES;

[self->thumbData release];

self->thumbData = nil;

}

超时或其他错误方法时:

   - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[self->thumbData release];
self->thumbData = nil;
self.thumbActivityView.hidden = YES;
[thumbConnection release];
self->thumbConnection = nil;

}

1 个答案:

答案 0 :(得分:0)

如果非零,请不要再分配。最好还是在对象的init方法中分配它,并在对象的dealloc方法中释放它