Is the main queue dispatched when the application is restarted in the background?

My application uses UIBackgroundMode , that is, it restarts iOS when Bluetooth events occur, even when the application is inactive. Therefore, Bluetooth events are sent by iOS to the background (the queue is indicated by me).

Is it possible to send the code back to the main queue, i.e.

DispatchQueue.main.async { } (Swift)

dispatch_async(dispatch_get_main_queue(), ^{ }) (Objective-C)

and suppose its start cycle is running, that is, my blocks are sent? Or is the main queue suspended in the background, and therefore should I avoid adding dispatch blocks to it?

+6
source share
1 answer

When sending to the main queue, it should be safe to be in the background.

When your application is in the background, all it does is technically run in the background because the system marks your application as a lower priority. However, for the system to run your code, it must at least have a main queue. Therefore, we can safely assume that you will have access to the main queue. You can create other work queues from there if you want, but everything will most likely be superimposed on one background thread to execute, so you may not see much benefit.

Also note that testing the background thread can be a bit complicated. Background threads are always executed and never seem to turn off during the simulator. The same thing happens when testing on a device if Xcode is connected and debugged. The system also allows your application to run continuously for 10 minutes (the last thing I checked, it may have changed in the last year or two) after entering the background, if necessary, and after that it will take something like the bluetooth event that you are talking about mentioned to get extra time in the background.

Source: bad experience with background applications.

+3
source

Source: https://habr.com/ru/post/1013981/


All Articles