12

I am developing an IOS app in objective c which displays the heart rate from bluetooth BLE with a corresponding graphic. If the app runs in the foreground everything forks fine without any problem.

But if the app goes in background mode the BLE measuring still continues (I am using "Uses Bluetooth LE accessories") but it is killed by the OS after some minutes or some seconds with the message "The app on iPhone quit unexpectedly --> Message from debugger: Terminated due to signal 9.

I can not find out why this happens only in background mode and not in the foreground when I see the app on the iPhone?

The CPU and memory usage is the same in foreground or background it is about 130% and 16 MB.

The app is killed after 2:40

enter image description here enter image description here

Ing. Ron
  • 2,005
  • 2
  • 17
  • 33
  • This is because your application is using too much memory in the background so the operating system kills the application. – SierraMike Apr 11 '15 at 21:33
  • But why does it only happens in the background mode? And 16 MB are not much isn't it? – Ing. Ron Apr 11 '15 at 21:36
  • From my understanding, an application in the foreground has more memory allocated to the application than the application in the background. According to the operating system that is a lot of memory for an application in the background. Do you have any code you could show us of your background tasks? – SierraMike Apr 11 '15 at 21:43
  • 1
    Also 130% CPU is pretty high for a foreground task, let alone a background task - given that you should be doing Bluetooth IO this implies that you have some other CPU bound loop - show some code or use Instruments to identify where your app is spending time. – Paulw11 Apr 11 '15 at 22:24
  • I added a screen print to my question which shows the memory and CPU usage. I have a very complex 2d graphic which is generated at runtime that is very CPU consuming. Is that graphic also generated in the background? It seems so? Maybe I can add an if else statement that only generates the graphic if the app is in foreground? It is not easy to post the code because there are many things done in the background: – Ing. Ron Apr 11 '15 at 22:48
  • The heart rate is read from bluetooth sensor, the graphic is generated (maybe also in the background?) and every 10 sec. some data is written the core data database which I later use for a graphic for the trainings session. Can you analyse something from the debugger graphics I posted? – Ing. Ron Apr 11 '15 at 22:48
  • 1
    Use the time profiler in Instruments - http://www.raywenderlich.com/23037/how-to-use-instruments-in-xcode but it sounds like you need your code to be aware when it is the background and stop rendering the image. – Paulw11 Apr 11 '15 at 23:00
  • Many thanks to all your fast answers! I made a quick test and YES if I stop rendering the image when the app is in background prevents my app to be killed from IOS. That means signal 9 termination is also used by too much memory usage! – Ing. Ron Apr 11 '15 at 23:09
  • Does anyone know what the CPU utilization has to be for this BT background mode to work? – Frost Sep 24 '21 at 20:51

1 Answers1

10

"All things which are done in the foreground" are done when the app is in background mode and the app is using by example "Uses Bluetooth LE accessories"! And if the CPU usage is too high iOS kills the app with "Terminated due to signal 9" not only if the memory usage is too high! (I think, this fact is missing in the Apple documentation).

To prevent the app to be killed while in background, stop doing high CPU using things, like rendering images, when the app is in background mode!

Erik van der Neut
  • 2,245
  • 2
  • 22
  • 21
Ing. Ron
  • 2,005
  • 2
  • 17
  • 33
  • in my app i have to upload location details every 15 seconds in background. so can u say me that, is this location sending method is creating problem or what??? – Patel Jigar Oct 19 '15 at 10:16
  • I think that should work, my problem was due to a complex graphical user interface which was refreshed every second even if the app was in background. – Ing. Ron Oct 19 '15 at 18:04