1

When I get app on background mode, app crashed with log below.

This is device log of crash :

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 

This the class implementation :

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
    }
  }
}

Any advice to fix this bug while application become in background mode ?

Maximelc
  • 2,384
  • 1
  • 21
  • 17
Duc Chinh
  • 13
  • 5
  • Hi, could you please add exception breakpoint (https://stackoverflow.com/a/17802942/2370587) and update your post with the code where Xcode paused – Maximelc Oct 25 '18 at 07:39
  • @Maximelc: This crash is in background mode, and when debug it don't show crash log – Duc Chinh Oct 25 '18 at 07:41
  • Could you provide some code where you manipulate thread ? using GCD (Grand Central Dispatch) maybe ? – Maximelc Oct 25 '18 at 07:44
  • Your AppDelegate.swift (near line 17) can be useful to get hint to face this issue – Maximelc Oct 25 '18 at 07:46
  • DispatchQueue.main.async { self.resultsNewspaper = DatabaseManager.shareInstance.getNewspaperLatest() self.resultsNewspaperRegion = DatabaseManager.shareInstance.getNewspaperRegion() self.tbvCity.rowHeight = self.view.layer.bounds.size.height / 8 self.tbvCity.reloadData() self.ColNewspaper.reloadData() } – Duc Chinh Oct 25 '18 at 07:51
  • It nothing in "Your AppDelegate.swift (near line 17)" this is the first code on Appdelegate – Duc Chinh Oct 25 '18 at 07:52
  • thanks @Maximelc, I had updated my question – Duc Chinh Oct 25 '18 at 08:13

1 Answers1

4

Your application used up too much time in the background or took too long to launch and was therefore terminated by the iOS watchdog process (happens on device only). This can be seen from the Termination Reason code. For more information, refer to this post: What does 8badf00d mean?

You should check if it's the start time or a background task that causes the crash. If it's the start time, you have to reduce your app startup time somehow. Apple has given a number of talks on this subject and corresponding videos can be found on the Apple Developer platform (among the WWDC videos from the last few years, e.g. this one: https://developer.apple.com/videos/play/wwdc2016/406).

For long running background tasks and how to implement them, refer to the Apple documentation: https://developer.apple.com/library/archive/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH4-SW3

Lutz
  • 1,734
  • 9
  • 18