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
wohenyingyu01
V2EX  ›  iDev

applicationDidEnterBackground 的代码一直在跑……

  •  
  •   wohenyingyu01 · 2016-02-19 15:29:45 +08:00 · 2769 次点击
    这是一个创建于 3204 天前的主题,其中的信息可能已经有所发展或是发生改变。
    说好只能跑 5 秒的,我就试了下,没想到根本停不下来,附代码:
    - (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"%@",NSStringFromSelector(_cmd));
    while (YES) {
    static int count=0;
    count++;
    [NSThread sleepForTimeInterval:1];
    NSLog(@"sleeping %d",count); //wtf it can run forever?
    }
    }

    按了 home 键之后一直打印到 80 几了还在跑,是不是有什么地方写错了……
    9 条回复    2016-02-25 18:01:48 +08:00
    sparanoid
        1
    sparanoid  
    MOD
       2016-02-19 15:30:28 +08:00
    moved to /go/idev
    icodesign
        2
    icodesign  
       2016-02-19 15:33:09 +08:00
    没人跟你说跑 5 秒
    iOSer2012
        3
    iOSer2012  
       2016-02-19 16:13:57 +08:00   ❤️ 1
    不应该对这个方法做这种测试,这个方法只是表明 application 到了后台。这个方法根据官方文档
    Your implementation of this method has approximately five seconds to perform any tasks and return. If you need additional time to perform any final tasks, you can request additional execution time from the system by calling beginBackgroundTaskWithExpirationHandler:. In practice, you should return from applicationDidEnterBackground: as quickly as possible. If the method does not return before time runs out your app is terminated and purged from memory.

    You should perform any tasks relating to adjusting your user interface before this method exits but other tasks (such as saving state) should be moved to a concurrent dispatch queue or secondary thread as needed. Because it's likely any background tasks you start in applicationDidEnterBackground: will not run until after that method exits, you should request additional background execution time before starting those tasks. In other words, first call beginBackgroundTaskWithExpirationHandler: and then run the task on a dispatch queue or secondary thread.
    更不应该在这个方法使用死循环来测试 application 会不会被系统 kill
    loveuqian
        4
    loveuqian  
       2016-02-19 16:15:54 +08:00 via iPhone
    @iOSer2012
    如果我想 app 在后台也干一些事
    这件事要一直干的,不能停
    正确的姿势应该是什么
    iOSer2012
        5
    iOSer2012  
       2016-02-19 16:27:31 +08:00
    iOS 支持的后台任务有限,基本上就是音频,位置相关 ,voip, 蓝牙

    ,详细看 Table 3-1 Background modes for apps
    https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
    还有一个中文的
    https://github.com/bboyfeiyu/iOS-tech-frontier/blob/master/issue-3/iOS%E5%90%8E%E5%8F%B0%E6%A8%A1%E5%BC%8F%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97.md
    iOS7 以后 又增加了几个:后台获取,远程通知的后台处理, URLSession 的后台传输的处理
    http://objccn.io/issue-5-5/
    iOSer2012
        6
    iOSer2012  
       2016-02-19 16:36:15 +08:00
    wohenyingyu01
        7
    wohenyingyu01  
    OP
       2016-02-19 17:02:51 +08:00
    @iOSer2012 我就是看文档说 approximately 5 seconds 的,为什么说不能测试呢?看到了就想去证实一下应该是本能反应吧…我知道如何延长时限,就是想试试被系统结束的极限条件是什么……
    wadahana
        8
    wadahana  
       2016-02-25 15:33:50 +08:00   ❤️ 1
    ios7 以上正常可以活 180s ; ios6 好像是 300 or 600s 忘了。
    [[UIApplication sharedApplication] backgroundTimeRemaining] 可以读到还能活多久
    后台下载用 NSURLSession
    如果只需要激活几次偷偷写数据,用 GPS
    wohenyingyu01
        9
    wohenyingyu01  
    OP
       2016-02-25 18:01:48 +08:00
    @wadahana 老司机!!!谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3478 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 04:55 · PVG 12:55 · LAX 20:55 · JFK 23:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.