BGProcessingTask expirationHandler - Do i MUST finish background work? or can it be resumed later?

43 Views Asked by At

I'm building a BGProcessingTask that may take more then 5 minutes to complete. The processing task runs on a Singleton object that can be accessed (or even started) when the app is on foreground.

When the task expirationHandler is called, do i must finish my background work before calling task.setTaskCompleted(success: false)?

I would like to have the background work suspended not cancelled. So when i reschedule another background processing task later or the app moves to foreground the same background task will continue.

Is it ok practice? Will iOS might kill the background process (and the app) if not finished work before calling task.setTaskCompleted()? Or can i trust it will only get suspended and be able to resume later?

Kind Regards

1

There are 1 best solutions below

0
On

When your expiration handler is called you should notify your processing task to stop its operations and call setTaskCompleted as soon as possible.

From the documentation:

Not calling setTaskCompleted(success:) before the time for the task expires may result in the system killing your app.

In your case, when your expiration handler is called you should instruct your object to stop its work, save its state so that it can resume at a later time and reschedule another processing task (if it has not already done so) before calling setTaskCompleted(success:false).

Your app may be terminated and relaunched in the background when the next BGProcessingTask starts, so you cannot rely on your app simply being suspended.