V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
alexrezit
V2EX  ›  iDev

除了 KVO / delegate / 定时刷新以外下载工具的进度更新用什么写法比较好?

  •  
  •   alexrezit · 2012-11-08 20:52:51 +08:00 · 2899 次点击
    这是一个创建于 4191 天前的主题,其中的信息可能已经有所发展或是发生改变。
    原则是性能越高越好. 求指点, 谢谢!
    目前用这几种方法在移动设备上的性能表现都不怎么好.
    16 条回复    1970-01-01 08:00:00 +08:00
    alexrezit
        1
    alexrezit  
    OP
       2012-11-09 16:37:57 +08:00
    没人么... orz
    txx
        2
    txx  
       2012-11-09 16:48:36 +08:00
    不太理解为何delegate效率低。。。。
    alexrezit
        3
    alexrezit  
    OP
       2012-11-09 16:50:39 +08:00
    @txx delegate 太频繁了, 实时更新 UI 速度慢得要死.
    Echoldman
        4
    Echoldman  
       2012-11-09 16:53:41 +08:00
    @alexrezit 别每个delegate 的回调都更新,按照一定的策略,分批更新。
    laihj
        5
    laihj  
       2012-11-09 17:43:23 +08:00
    同 @Echoldman 自己写代码降低刷界面的频率,更新UI是主线程事件,你用什么方法都是卡的
    alexrezit
        6
    alexrezit  
    OP
       2012-11-09 19:17:53 +08:00
    @laihj @Echoldman 不知道有什么好的办法能实现... 分批的话, 也不知道怎样写比较合适.
    wtl
        7
    wtl  
       2012-11-09 19:51:35 +08:00
    可以考虑用GCD 以前碰到过类似问题 需要频繁更新UI 不过没在iOS上试过
    static NSOperationQueue *updateQueue;
    if ([updateQueue operationCount] < 2) {
    [updateQueue addOperationWithBlock:^{
    dispatch_sync(dispatch_get_main_queue(), ^{
    updateUI();
    });
    }];
    }
    alexrezit
        8
    alexrezit  
    OP
       2012-11-09 20:27:38 +08:00
    @wtl

    Thanks!
    我现在是这么写的:
    https://gist.github.com/4045445
    alexrezit
        9
    alexrezit  
    OP
       2012-11-09 20:28:55 +08:00
    krafttuc
        10
    krafttuc  
       2012-11-09 20:34:05 +08:00
    没看懂楼主到底想表达什么意思。你是说,在做网络I/O的时候在UI上显示当前进度吗?delegate跟性能不性能没什么关系啊,就是正常的函数调用。你卡UI很可能就是在主线程做了一些比较耗费时间的事情,把他们扔到后台去。
    wtl
        11
    wtl  
       2012-11-09 21:03:41 +08:00
    @alexrezit :)不谢。不过,我觉得你那种写法与timer没多大不同。
    我的本意是向updateQueue中加入更新的任务,并且为了避免无谓的更新,维持其个数小于2两个,而又充分利用CPU,尽可能的及时更新。
    但是对于你的需求,相对于下载时间,一秒其实无所谓,所以也许timer更适合。

    BTW,怎么粘帖code啊?
    wtl
        12
    wtl  
       2012-11-09 21:09:42 +08:00
    另外,我的那段代码是要加到delegate或者kvo的回调里,或者加到timer中,都可以。
    alexrezit
        13
    alexrezit  
    OP
       2012-11-09 21:14:01 +08:00
    @wtl
    用 github 的 gist 链接粘贴代码, 不过必须是 http 的.
    我都不确定自己还会不会用 NSTimer, dispatch 已经把我变成了一个懒人... orz
    现在暂时还没试真机的表现呢, 因为测试下载功能要占用大量的空间, Apple 又非常脑残地不能从 Xcode 直接清除测试机上的数据.
    luanma
        14
    luanma  
       2012-12-02 08:58:02 +08:00
    减少刷新频率
    dingtianran
        15
    dingtianran  
       2012-12-16 19:31:25 +08:00
    1.)
    10.7及之前的mac有一个web sharing选项,其实是个内置的apache,用来快速做一个站点,非常适合于真机测试
    10.8之后给关掉了,可以通过改一些设置再度打开。

    同时配合真机的networking link condition(iOS 6才有)可以很方便模拟巨慢巨卡的网速来测试下载进度

    2.)
    刷新UI的过程可不可以拆开,拆成好多个phase1,2,3,4,5 然后准备相应好多个BOOL,如果某个phase需要刷新设置成YES,平时都是NO
    绘制的时候遇到YES才会去update,如果NO就路过了
    alexrezit
        16
    alexrezit  
    OP
       2012-12-16 20:01:18 +08:00
    @dingtianran
    谢谢你的回复.
    其实 Prefspane 形式的 Network Link Conditioner 在开发者组件里面就有的.
    我现在用的还是定时刷新的方式.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2249 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 04:51 · PVG 12:51 · LAX 21:51 · JFK 00:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.