2012-11-08 62 views
0

我試圖做一個請求,但它只能在第一時間....NSURLConnect工作只有1次

這裏是我的代碼:

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時: - (無效)connectionDidFinishLoading:(NSURLConnection的*)連接{

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

第二次或第三次會發生什麼? – trojanfoe

+0

連接無響應 – Ladessa

+1

您使用ARC還是MRR? – trojanfoe

回答

0

,如果它是非零不要再分配它。更好的是在你的對象的init方法中分配它並釋放它在你的對象的dealloc方法中