NSURLConnection的default runloop mode是NSEventTrackingRunLoopMode

原文地址:NSURLConnection的default runloop mode是NSEventTrackingRunLoopMode作者:jetKill


据https://github.com/rs/SDWebImage的作者rs描述:
This worked quite well and allowed me to concentrate on other parts of my application. But when I started to compare my application with its direct competitor - the built-in Youtube application - I was very unhappy with the loading speed of the images. After some network sniffing, I found that every HTTP requests for my images was 10 times slower than Youtube's ones... On my own network, Youtube was 10 time faster than my own servers... WTF??

In fact, my servers were well but a lot of latency was added to the requests, certainly because my application wasn't responsive enough to handle the requests at full speed. At this moment, I understood something important, asynchronous NSURLConnections are tied to the main runloop in the NSEventTrackingRunLoopMode. As explained in the documentation, this runloop mode is affected by UI events:

Cocoa uses this mode to restrict incoming events during mouse-dragging loops and other sorts of user interface tracking loops.

A simple test to recognize an application using NSURLConnection in its default mode to load there remote images is to scroll the UITableView with your finger to disclose an unloaded image, and to keep your finger pressed on the screen. If the image doesn't load until you release you finger, you've got one (try with the Facebook app for instance). It took me quite some time to understand the reason for this lagging issue.

一般情况下,我们使用NSURLConnection时,都使用如下的代码:

_connection = [[NSURLConnection alloc] initWithRequest:URLRequest delegate:self];
这段代码创建的connection运行在main runloop以及NSEventTrackingRunLoopMode下,而该模式在mouse-dragging loops and other sorts of user interface tracking loops期间会阻止对该connection的代理通知事件,比如在UITableView滚动的时候,connection的代理方法就不会被通知。这不是我们想要的,如果想要在mouse-dragging loops and other sorts of user interface tracking loops期间我们的connection的代理方法仍然被调用,可用以下代码替换上面的代码:



_connection = [[NSURLConnection alloc] initWithRequest:URLRequest delegate:self startImmediately:NO];
[_connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[_connection start];
可以用https://github.com/rs/SDWebImage中的开放框架做个测试。

Cocos will freeze while working with a UIScrollView by default. Luckily the location to you need to modify is noticeably documented in the Cocos source code. In Director.mlook for this block of comment with code…




//
// If you want to attach the opengl view into UIScrollView
// uncomment this line to prevent 'freezing'. It doesn't work on
// with the Fast Director
//
// [[NSRunLoop currentRunLoop] addTimer:animationTimerforMode:NSRunLoopCommonModes];
if( ! [CCDirector setDirectorType:kCCDirectorTypeDisplayLink] )
[CCDirector setDirectorType:kCCDirectorTypeNSTimer];

You need to force it to set the director type to kCCDirectorTypeNSTimer to prevent cocos2d from “freezing” while scrolling. Either change
the line in the macro, or copy the macro contents to your appDidFinishLaunching method, and change the line above to simply:

[CCDirector setDirectorType:kCCDirectorTypeNSTimer] ;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值