后台线程崩溃

时间:2018-10-25 07:35:51

标签: ios swift grand-central-dispatch

当我在后台模式下运行应用程序时,应用程序崩溃并显示以下日志。

这是崩溃的设备日志:

Exception Type:  EXC_CRASH (SIGKILL)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Termination Reason: Namespace ASSERTIOND, Code 0x8badf00d
Triggered by Thread:  0

Filtered syslog:
None found

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libsystem_kernel.dylib        0x0000000183563de8 mach_msg_trap + 8
1   libsystem_kernel.dylib          0x0000000183563c60 mach_msg + 72
2   CoreFoundation                  0x0000000183aa6e40 __CFRunLoopServiceMachPort + 196
3   CoreFoundation                  0x0000000183aa4908 __CFRunLoopRun + 1568
4   CoreFoundation                  0x00000001839c4da8 CFRunLoopRunSpecific + 552
5   GraphicsServices                0x00000001859aa020 GSEventRunModal + 100
6   UIKit                           0x000000018d9e4758 UIApplicationMain + 236
7   AijouNetto                      0x00000001008a851c main + 410908 (AppDelegate.swift:17)
8   libdyld.dylib                   0x0000000183455fc0 start + 4 

这是该类的实现:

class EKNBackgroundTaskManager {

let backgroundDQ = DispatchQueue.global(qos: .background)
var backgroundUpdateTask: UIBackgroundTaskIdentifier!

init(withName: String) {

    self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(withName: withName) {}
}

func runBackgroundTask(withCode: @escaping (_ cH: @escaping () -> Void) -> Void)
{
    backgroundDQ.async {
        withCode() {
            self.endBackgroungTask()
        }
    }
}

func endBackgroungTask() {
    if backgroundUpdateTask != nil && backgroundUpdateTask != UIBackgroundTaskInvalid {
        UIApplication.shared.endBackgroundTask(backgroundUpdateTask)
        backgroundUpdateTask = UIBackgroundTaskInvalid
    }
  }
}

在应用程序进入后台模式时是否有任何修复此错误的建议?

1 个答案:

答案 0 :(得分:3)

您的应用程序在后台花费了太多时间,或者启动时间太长,因此被监视程序终止。 这可以从终止原因代码中看到。 有关更多信息,请参阅这篇文章:What does 8badf00d mean?

您应该检查是导致崩溃的开始时间还是后台任务。有关长时间运行的后台任务以及如何实现这些任务,请参阅Apple文档:https://developer.apple.com/library/archive/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH4-SW3