Iphone 从后台返回后,计时器循环偶尔崩溃

Iphone 从后台返回后,计时器循环偶尔崩溃,iphone,ios6,Iphone,Ios6,我在最新发布的应用程序中遇到了一个一致的问题。我有一个在AppDelegate中运行的计时器,它每30秒调用一个函数来加载新广告。我想这是这次车祸的罪魁祸首。使用Critercism,我已经有13个用户发生了20多次崩溃。用户大多使用IOS 6或其变体。这是它给我的日志: 塞格沃·阿克尔 0 libobjc.A.dylib 0x3acd25b0 objc_msgSend+16 1基金会0x33 94B27 7 2 CoreFoundation 0x330125dfCFRUNLOOP\u正在调用

我在最新发布的应用程序中遇到了一个一致的问题。我有一个在AppDelegate中运行的计时器,它每30秒调用一个函数来加载新广告。我想这是这次车祸的罪魁祸首。使用Critercism,我已经有13个用户发生了20多次崩溃。用户大多使用IOS 6或其变体。这是它给我的日志:

塞格沃·阿克尔

0 libobjc.A.dylib 0x3acd25b0 objc_msgSend+16

1基金会0x33 94B27 7 2 CoreFoundation 0x330125dfCFRUNLOOP\u正在调用\u OUT\u到\u计时器\u回调\u函数+15

3 CoreFoundation 0x3301291\uu CFRunLoopDoTimer+273

4 CoreFoundation 0x33010f01\uuuu CFRunLoopRun+1233

5 CoreFoundation 0x32f83ebd CFRunLoopRunSpecific+357

6 CoreFoundation 0x32f83d49 CFRUNLOOPSRUNINMODE+105

7图形服务0x36b452eb GSEventRunModal+75

8 UIKit 0x34e99301 UIApplicationMain+1121

9 AutoScene 0x000031b7 main(main.m:7)

我的计时器是:

[NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(resetAdTimer) userInfo:nil repeats:YES];
我不知道这是否是同一个问题,但我经历过从后台返回应用程序时,应用程序只是挂起。。它似乎也挂了30秒,这让我相信这是计时器代码

这是管理广告抓取的糟糕方法吗?当计时器代码长时间进入后台时,它会出错吗


谢谢你的帮助

您应该在appWillResignActive中使计时器无效,并在appWillResignActive中再次创建它

您应该使用此方法暂停正在进行的任务、禁用计时器和 降低OpenGL ES的帧速率。游戏应该使用这种方法 暂停比赛。处于非活动状态的应用程序应执行最少的操作 在等待转换到活动或背景时工作 国家


对于Swift 4.2

override func viewDidLoad() {
        let notificationCenter = NotificationCenter.default
        notificationCenter.addObserver(self, selector: #selector(appWillResignActive), name: UIApplication.willResignActiveNotification, object: nil)
        notificationCenter.addObserver(self, selector: #selector(appBecomesActive), name: UIApplication.didBecomeActiveNotification, object: nil)
}


@objc func appWillResignActive() {
    //to background
}

@objc func appBecomesActive() {
    //to front
}
别忘了移开观察者

    notificationCenter.removeObserver(self, name: UIApplication.willResignActiveNotification, object: nil)
    notificationCenter.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
灵感来自这里

    notificationCenter.removeObserver(self, name: UIApplication.willResignActiveNotification, object: nil)
    notificationCenter.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)