2
votes

I have UIImageView inside UIScrollView. UIImageView displays image which is constantly downloaded from the internet (stream opened by using NSURLConnection).

NSString surl = @"some valid url";
NSURL* nsurl = [NSURL URLWithString:surl];
NSURLRequest *request = [NSURLRequest requestWithURL:nsurl];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

// this method will be called when every chunk of data is received
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // here I collect data and when i have enough
    // data to create UIImage i create one
}

Every time I receive data I pack it into a NSData and when I receive whole image I create UIImage from this NSData and then I set this image as image of UIImageView. Everything works fine, image updates as it should but when I touch screen and start to move content around (this UIImageView is always bigger than the screen and UIScrollView always fit whole screen) image updates stop and I see only last frame that was displayed at the time i clicked. This seems to be happening because my

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

stops being called. After releasing my finger from the screen method again receives data and image updates as it should. I have no idea why my NSURLConnection delegate method stops receiving data. Any help?

2

2 Answers

4
votes

The documentation for -[NSURLConnection initWithRequest:didReceiveData:] says this:

By default, for the connection to work correctly, the calling thread’s run loop must be operating in the default run loop mode.

I assume you are creating the NSURLConnection on the main thread.

When a UIScrollView is tracking a touch, it runs the run loop in different mode, not the default mode. That's why you stop getting updates while you're touching the scroll view. (This also causes NSTimers to stop firing while you're touching a scroll view.)

I think you can set up your NSURLConnection to run in both the default mode and the scroll view tracking mode like this:

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
    delegate:self startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode: NSRunLoopCommonModes];
[connection start];
0
votes

Sounds like your user interaction is blocking your data update because they are both running on the main thread. I happen to have responded to a very similar question a couple not long ago.