Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does main queue get dispatched when app re-launches into background?

My app makes use of UIBackgroundMode, i.e. it gets relaunched by iOS when Bluetooth events occur even while the app is not active. Therefore Bluetooth events are dispatched by iOS onto a background queue (queue is specified by me).

Can I however dispatch code back to the main queue, i.e.

DispatchQueue.main.async { } (Swift)

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

and assume that its run loop is running, that is my blocks get dispatched? Or is the main queue suspended in background mode and thus I should avoid adding dispatching blocks to it?

like image 744
Lars Blumberg Avatar asked Jan 11 '17 09:01

Lars Blumberg


People also ask

Should I sync on main queue?

NEVER call the sync function on the main queue. If you call the sync function on the main queue it will block the queue as well as the queue will be waiting for the task to be completed but the task will never be finished since it will not be even able to start due to the queue is already blocked.

Is Main queue a serial queue?

so the main queue is a serial queue, and [self doOne] , [self doTwo] , [self doThree] are executed sequentially in that order. Also, it has to be serial since the blocks run on the same thread.

What is Dispatch main async?

From Stack Overflow. Essentially this just means that sync will block the main thread until the task has finished, async means this will happen on a background thread and update the main thread when it is finished.

What is DispatchQueue in IOS?

An object that manages the execution of tasks serially or concurrently on your app's main thread or on a background thread.


1 Answers

It should be safe to dispatch to the main queue while in the background.

When your app is in the background, everything it does is technically done on background threads because the system marks your app as a lower priority. However, in order for the system to let your code run it needs to at least have a main queue. Therefore, it is safe to assume 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 shoved onto the one background thread for execution, so you may not see much benefit.

Also note that testing background threading can be a little tricky. Background threads will always be executed and seem to never be shut down while in the simulator. The same happens when testing on a device if Xcode is connected and debugging. The system also lets your app run continuously for 10 minutes (last I checked, it's possible this has changed in the last year or two) after entering the background if needed, and after it will require something like the bluetooth event you mentioned to get additional time in the background.

Source: An unfortunate amount of experience dealing with backgrounded apps.

like image 175
esthepiking Avatar answered Oct 27 '22 10:10

esthepiking