Reputation: 357
I have a TTTableView with a bunch of TTImageViews in it. The imageviews pull their images from a remote server. I can view all of the images in a browser just fine. They all load very quickly.
For some reason, about 10% of the TTImageViews time out when trying to load an image. The thing is, they time out really, really fast. Like under a second. When I load the TTTableView, I immediately get NSErrors like this (note: I've changed the server and image name):
Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x84232f0 {NSErrorFailingURLStringKey=http://www.myserver.com/myimage.jpg, NSErrorFailingURLKey=http://www.myserver.com/myimage.jpg, NSLocalizedDescription=The request timed out., NSUnderlyingError=0x849e640 "The request timed out."}
So, any idea why the TTImageViews sporadically time out immediately?
Upvotes: 0
Views: 183
Reputation: 300
I am not sure if this has something to do with that problem, but it sounds that could be related.
The problem I was having was that when 2 (or more) simultaneous requests to the same image URL occurred, the images wouldn't load in any of the requests. And the error was always the same you reported:
Code=-1001 "The request timed out."
I have found that in TTRequestLoader.m on the method
- (void)connectToURL:(NSURL*)URL
when there are 2 or more requests to the same URL, the code is:
TTURLRequest* request = _requests.count == 1 ? [_requests objectAtIndex:0] : nil;
NSURLRequest* URLRequest = [_queue createNSURLRequest:request URL:URL];
So request variable results in "nil" value, and createNSURLRequest fails to set the timeout.
Modifying it to:
TTURLRequest* request = [_requests lastObject];
NSURLRequest* URLRequest = [_queue createNSURLRequest:request URL:URL];
Seems to work OK
Hope this helps!
Upvotes: 1
Reputation: 41
yep! it is weird sometimes because of the network problem. Here is my solution, I let the class to conform to TTImageViewDelegate and in the - (void)imageView:(TTImageView*)imageView didFailLoadWithError:(NSError*)error method, I use a dictionary to record how many times that it fails to load specific image.If the failure times is not over 3, then I call [imageView reload] to force Three20 to rerequest the image again. Usually it works well, I mean sometimes when it requests second time, the image is loaded successfully. While if it requests failed for consecutive 3 times, then just giving up to request that image, which means at that time the network is so bad.
#pragma mark TTImageView Delegate
- (void)imageView:(TTImageView*)imageView didLoadImage:(UIImage*)image{
[failLoadedImagesDict removeObjectForKey:imageView.urlPath];
}
/**
* Called when the image failed to load asynchronously.
* If error is nil then the request was cancelled.
*/
- (void)imageView:(TTImageView*)imageView didFailLoadWithError:(NSError*)error{
NSString *urlPath = imageView.urlPath;
NSInteger count = [[failLoadedImagesDict valueForKey:urlPath] integerValue];
if(count > 2) {
NSLog(@"Fail to load image for 3 times with error: %@", error);
[failLoadedImagesDict removeObjectForKey:urlPath];
return;
}
count++;
[failLoadedImagesDict setObject:[NSNumber numberWithInteger:count] forKey:urlPath];
[imageView reload];
}
Upvotes: 1