I have a NSOperation that downloads some data using NSURLConnection, it looks somewhat like this:
....
- (void)main
{
....
while (!self.isCancelled && !self.isLoaded)
{
[NSRunloop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NDate distantFutute]];
}
....
}
The problem is that from time to time connection hangs and since there are no other sources firing, the loop keeps running forever, leaving no chance to cancel the operation. Since there is a limit for 1 operation in my operation queue, everything is pretty much stuck.
The question is what would be the best solution to this problem.
- Instead of distantFuture use a smaller time period like half a second. (Will probably eat extra cpu cycles)
- Try to store a reference to running thread in ivar and tickle run loop via perform selector on thread in operartion's cancel method. (Can result in situation when we queue a selector on a run loop which has already exited and will no longer run, which results in a leak as far as i know)
Or maybe there is another way?