1

I have a problem when my application goes into the background. I need to send the images for processing to the intelligence to wait for the intelligence to process and save them with me. However, the problem is when I start sending and pressing the home button and when my application goes into the background. How can I do this to end the process , whether or not the application goes into the background.

I added this before my function start send image.

DispatchQueue.global().async {
  // Request the task assertion and save the ID.
  self.backgroundTaskID = UIApplication.shared.
             beginBackgroundTask (withName: "Finish Network Tasks")  {
     // End the task if time expires.
     UIApplication.shared.endBackgroundTask(self.backgroundTaskID!)
     self.backgroundTaskID = UIBackgroundTaskInvalid
  }

and added this when all image is procesed

    UIApplication.shared.endBackgroundTask(self.backgroundTaskID!)
    self.backgroundTaskID = UIBackgroundTaskIdentifier.invalid

and i tested it work

Stefan
  • 69
  • 6
  • 2
    Your question is not clear to me, can you please rephrase it. Please at the screenshot to support your theory – ketaki Damale Mar 24 '20 at 13:33
  • @ketakiDamale I need to send 10 images to intelligence. When the application is in Foreground everything works. But when I start sending and pressing the home button while the application is in the background nothing happens, everything is suspended. Is there any possibility that when the application goes in the background it will not be suspended. – Stefan Mar 24 '20 at 13:38
  • Please post your code snippet for the same. – ketaki Damale Mar 24 '20 at 13:56
  • check https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/extending_your_app_s_background_execution_time – timbre timbre Mar 24 '20 at 13:59

1 Answers1

1

I used to find a solution for background tasks over a long time. Ok, I had some special requirements for this, but you can have a look at this question here: Background task in iOS action extension

At the end, I found a simple solution that is described here: https://mixable.blog/background-tasks-in-ios/ ... This should work for different tasks (for apps and also extensions) that need to be performed in the background. Hope this helps!

mixable
  • 1,068
  • 2
  • 12
  • 42