I have been searching for this problem on the SOF for several days and I still have not found the solution (say the same problem) yet.
I'm making and app that downloads 5 images simultaneously in an URL list (each image is on a different server).
I have an ImageDownloader
class subclasses NSOperation
and implements the NSURLConnectionDataDelegate
.
So that I can add an instance of ImageDownloader
to an operationQueue
in the ViewController
and it will run in a separate thread under the operationQueue
. The line that add the downloader to the operationQueue
is here:
downloader = [[ImageDownloader alloc] init];
[downloader downloadImageWithURL:[controller.URList objectForKey:[NSString stringWithFormat:@"%d",downloadIndex]] queue:queue andTag:downloadIndex + 100]; //my custom initialize
downloader.delegate = self;
[queue addOperation:downloader]; //use the addOperation method
Everything works fine in iOS6 but messed up in iOS5 (5.0 on my test device and 5.1 on my SDK), it just doesn't receive any response nor data by performing the methods didReceiveResponse
and didReceiveData
at all (these 2 methods are not jumped in).
After the timeout was exceeded, the runloop jumps into didFailWithError
method and the program stalls.
As I understand, this means the runloop still runs right?
I tried to print out the error
and all I got is: The request timed out
.
When I reduce the number of downloading instances to 2 then it runs, but not with >=3 downloading instances.
One more information is that my network connection does limit the number of connection. But it work fine in iOS6, why it just doesn't work on iOS5?
I can still load the web in the simulator while the app is downloading.
So what kind of problem is this and how can I get over this problem?
Thanks in advance.
*Update:* as there are many classes and the problem's not been clearly detected yet, I will share here the whole project. You can download it directly from here: DownloadingImage
As I just found out, if you're using credentials there is a chance that the server will reject them randomly every once in a while. So if you have a check to make sure previousFailureCount == 0 then you will most likely have a bug.
I've just figured out where my problem is, but not really understand why.
In my
ImageDownloader
class, I set up a runloop withdone
andcurrentRunLoop
variables. In the main method, I have a while loop for forcing thecurrentRunLoop
run.As I remove those "runLoop" stuffs, the app runs smoothly on both iOS6 and iOS5.
So change the entire
ImageDownloader.m
with these lines then it works (I commented out some useless (say harmful) lines):Thank you guys for your supports.
==================================================================================
P/s: for anyone who interested in this problem, I update here my entire solution: DownloadImage_Final