开发者

iphone - Why the NSURLConnection connectionDidFinishLoading is not running?

开发者 https://www.devze.com 2023-01-22 04:16 出处:网络
My app looks like this: I have a UIScrollView and there is imageview inside. I implemented the scrollViewDidScroll delegate so that if th开发者_JAVA百科e scroll did scroll, I will create a NSURLConne

My app looks like this:

I have a UIScrollView and there is imageview inside. I implemented the scrollViewDidScroll delegate so that if th开发者_JAVA百科e scroll did scroll, I will create a NSURLConnection and download a data from some url, for only once.

The problem is that the NSURLConnection connectionDidFinishLoading only be called or run after I release my finger on the scroll view.

To be more clearly, If I use my finger to scroll the scrollview, I confirm that the NSURLConnection is created, but as long as I do not release the scrollview, the connectionDidFinishLoading is never called. Once I release my finger, the delegate of connectionDidFinishLoading is called.

Anyone can explain this to me please?

Thanks


That's because during scrolling the UI enters a modal loop processing the user scroll inputs, and until scrolling finishes your thread won't get back to the message queue to pick up any delegate messages from the NSURLConnection.

You could run the NSURLConnection in a separate thread, which should keep pumping messages when the user is scrolling, but you'd still not be able to call across to the UI thread. The way round that might be to have the connection thread write the data into a lock-free data structure that the UI thread can then read - but this is likely to be complicated!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号