1

I am working on a project which includes following functionalities.

  1. Location fetch and send to server in every 60 seconds.
  2. Audio/Video Calls.

The background modes which are set for the project are mentioned as under enter image description here

iOS: 14.1 Xcode: 12.1 Swift: 4

Problem: Whenever I put the app in background it fetches location or call for sometime then I get following error in logs. Whenever I put the app in background when audio call is going on then audio works for some time and after few seconds following error arise.

Message from debugger: Terminated due to signal 9.

How ever all things work fine when the application is in foreground. Application fetches location and call works.

Kindly suggest what additional I have to do or anything wrong am I doing.

Sanchit Paurush
  • 6,114
  • 17
  • 68
  • 107
  • Sounds like you are testing while running from Xcode. – matt Dec 22 '20 at 04:58
  • @matt Yes Correct. But it doesnt work even if I am not testing. App getting restarted. – Sanchit Paurush Dec 22 '20 at 05:01
  • Well you need to look at why it is getting restarted. It sounds like your app is being killed in the background for some reason. – matt Dec 22 '20 at 05:22
  • @matt Yes I checked but not able to find the reason of termination. I am getting this message in .ips file 48 seconds cpu time over 53 seconds (91% cpu average), exceeding limit of 80% cpu over 60 seconds – Sanchit Paurush Dec 22 '20 at 05:26
  • Well there's your answer. – matt Dec 22 '20 at 05:36
  • @matt How to know where is the app taking more CPU? – Sanchit Paurush Dec 22 '20 at 05:41
  • Instruments will tell you. – matt Dec 22 '20 at 05:44
  • @matt Please help me find solution. How can I find the issue. – Sanchit Paurush Dec 22 '20 at 05:56
  • 1
    I don't know what you want from me. You have shown no code. But you know the source of the issue (too much background CPU usage), and you know how to explore CPU usage (use Instruments). It's just a matter of doing the work. – matt Dec 22 '20 at 05:58
  • @matt I dont how how to explore CPU Usage using instruments and find where is it taking more CPU. I am asking for help there. – Sanchit Paurush Dec 22 '20 at 06:48
  • @SanchitPaurush do you try with this methods for task in background mode ? `UIApplication.shared.beginBackgroundTask(expirationHandler: {})` and `UIApplication.shared.endBackgroundTask(taskID)` when `TaskID` is an `UIBackgroundTaskIdentifier` returned by the begin background method. – iGhost Nov 24 '21 at 23:53

1 Answers1

0

The comment thread on your question suggested that the termination is due to excessive background CPU usage.

Based on your last comment, it sounds like you don't know where to start with Instruments (I've been there) as another commenter recommended, so I'll give some basic info on how to get started with CPU profiling in instruments, and then you can seek out more detailed tutorials online (this WWDC video from Apple is as good a place to start as any: https://developer.apple.com/videos/play/wwdc2019/411/#:~:text=Instruments%20is%20a%20powerful%20performance,optimize%20their%20behavior%20and%20performance )

The following assumes using Xcode 12.1 and its corresponding Instruments version 12.1, but most recent versions should be fairly similar (maybe a button is slightly differently placed, etc. in older versions):

  1. Open your app project in Xcode, and run it on a real device (simulators will give you information about your mac's CPU usage and will be very different to a real device).
  2. Go to the Debug navigator in the left sidebar (Cmd+7), select CPU in the sidebar, then click the Profile in Instruments button on the top right.
  3. Select 'Profile Current Session' if asked.
  4. Instruments should launch and start recording automatically.
  5. Reproduce the issue on your device.

Now to understand what's being shown in Instruments:

  1. The top pane (the moving chart) shows your CPU usage over time.
  2. The bottom pane shows the call tree of processes that have run.

There's a lot of info there, so you want to look at the Filter and configuration bar at the very bottom of the window, and select all the options in the Call Tree menu in the first instance. It looks like this: Call Tree Configuration Controls

Here's a short explanation of each of those options:

  • Separate by Thread: Shows the processes by thread to help diagnose overworked threads
  • Invert Call Tree: Reverses the stack to show the bottom portion first which is likely more useful for troubleshooting
  • Hide System Libraries: Removes system library processes so there's less noise & you can focus only on your app's code
  • Flatten Recursion: Combines recursive calls into one single entry
  • Top Functions: Combines the time the called function used, plus the time spent by functions called from that function. This can help you find your most expensive methods in terms of CPU usage.

Now you've got a filtered list of expensive CPU methods for your app only, and selecting a row gives you more information in the Extended Details pane to the right of the call tree view. This can show you exactly which method in which file of your code was running and even take you to it in Xcode (with a few button clicks).

Hopefully that should be enough to get you started, recognise some potential problem areas in your code that may be the cause of your app being terminated.

Gsp
  • 401
  • 5
  • 10