I got a crash while using BGTask / BGAppRefreshTask

  I'm trying to do some network requests in a BGAppRefreshTask's lanchHandler, but I'm getting some crashes. It is hard to recreate the crash on my now device. The crash count / DAU is about 1% according to log, by the way, my app is running under an enterprise account.

The last lines of the crash log is like:

0 CoreFoundation 0x0000000196475e48 __exceptionPreprocess + 164
1 libobjc.A.dylib 0x000000018f7478d8 objc_exception_throw + 60
2 Foundation 0x0000000190d3694c _userInfoForFileAndLine + 0
3 BackgroundTasks 0x00000001feb7fcfc -[BGTaskScheduler _callRegisteredHandlersForActivities:] + 528
4 BackgroundTasks 0x00000001feb7d184 -[BGTaskScheduler _handleAppLaunch] + 324
5 BackgroundTasks 0x00000001feb7cfdc __50+[BGTaskScheduler _applicationDidFinishLaunching:]_block_invoke + 52
...

It also shows: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'No launch handler registered for task with identifier com.my.identifier’

I believe I have done all the necessary steps(I setup according to the demo: https://developer.apple.com/documentation/backgroundtasks/refreshing_and_maintaining_your_app_using_background_tasks) to setup BGTask, I call BGTaskScheduler’s register method in the app's didFinishLaunchingWithOptions, and call BGTaskScheduler’s submit method in the app's applicationDidEnterBackground method, I also call BGTaskScheduler’s submit method in the BGTask's launchHandler(because I want to trigger the task more than one time after into background).

Since the error info is No launch handler registered, but I actually call register in the didFinishLaunchingWithOptions, is it possible that the register method failed for other some reason except for not adding the identifier in the info.plist?

Does anyone know why the launchHandler is not registered? What do I miss?

Thank you all in advance

Another interesting thing is that I try to test Apple's BGTask demo(https://developer.apple.com/documentation/backgroundtasks/refreshing_and_maintaining_your_app_using_background_tasks ) according to this Apple’s Document: https://developer.apple.com/documentation/backgroundtasks/starting_and_terminating_tasks_during_development, but every time I got a crash when try to trigger two or more BGTasks in a row, here is the steps I do the test:

  1. Put a breakpoint one line after BGTaskScheduler’s submit method in didFinishLaunchingWithOptions.
  2. Run my app on my device and put it into background, the code is blocked by the breakpoint.
  3. Use _simulateLaunchForTaskWithIdentifier to trigger a BGAppRefreshTask
  4. Resume the app, the desired task is called, also the BGTaskScheduler’s submit method is called from this task's launch handler, code is blocked again by the breakpoint I put in step 1.
  5. Use _simulateLaunchForTaskWithIdentifier to trigger another BGTask, crash, the description is also *** Terminating app due to uncaught exception
  6. 'NSInternalInconsistencyException', reason: 'No launch handler registered for task with identifier {public}@'

This crash is almost certain to happen, is it a bug for using iOS's private API?

I got a crash while using BGTask / BGAppRefreshTask
 
 
Q