mad*_*die 7 networking objective-c ios nsurlsession
在我的应用程序中,我尝试从服务器下载一批图像。
我遇到许多错误,其中一个我找不到解释。我检查了Apple的URL加载系统错误代码,但无法对错误代码12进行罚款。我也得到了HTTP load failed (error code: -999)(是NSURLErrorCancelled)和Task finished with error - code: -1001(是NSURLErrorTimedOut)。
我最近从使用过时的NSURLConnection切换为NSURLSession。对于前者,我也遇到了999和1001错误,但是我试图找出错误代码12的含义。
- (void)loadImage:(LeafletURL*)leafletURLInput isThumbnail:(BOOL)isThumbnailInput isBatchDownload:(BOOL)isBatchDownload isRetina:(BOOL)isRetina
{
isRetina_ = isRetina;
if (session)
{
/*is this the right call here? */
[session invalidateAndCancel];
[session release];
session = nil;
}
if (mImageData)
{
[mImageData release];
mImageData = nil;
}
self.leafletURL = leafletURLInput;
self.isThumbnail = isThumbnailInput;
NSString* location = (self.isThumbnail) ?leafletURL.thumbnailLocation :leafletURL.hiResImageLocation;
//// Check if the image needs to be downloaded from server. If it is a batch download, then override the local resources////
if ( ([location isEqualToString:kLocationServer] || (isBatchDownload && [location isEqualToString:kLocationResource])) && self.leafletURL.rawURL != nil )
{
//NSLog(@"final loadimage called server");
//// tell the delegate to get ride of the old image while waiting. ////
if([delegate respondsToSelector:@selector(leafletImageLoaderWillBeginLoadingImage:)])
{
[delegate leafletImageLoaderWillBeginLoadingImage:self];
}
mImageData = [[NSMutableData alloc] init];
/*download tasks have their data written to a local temp file. It’s the responsibility of the completion handler to move the file from its temporary location to a permanent location.*/
NSURL* url = [NSURL URLWithString:[leafletURL pathForImageOnServerUsingThumbnail:self.isThumbnail isRetina:isRetina]];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
session = [NSURLSession sharedSession];
dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
// do something with the data
}];
[dataTask resume];
}
//// if not, tell the delegate that the image is already cached. ////
else
{
if([delegate respondsToSelector:@selector(leafletImageLoaderDidFinishLoadingImage:)])
{
[delegate leafletImageLoaderDidFinishLoadingImage:self];
}
}
}
Run Code Online (Sandbox Code Playgroud)