First of all I'd like to say sorry in case you consider my question dummy, I'm new to iOS and multithreading and just want to understand how things are going on. As far as I know didEnterBackground
is the last function that iOS calls before app suspension and the app has about 5 secs to return from it otherwise iOS will kill the app. I'm currently thinking about such a situation - I have some task that is happening on the background thread(e.g. network download) and its completion block happens on the main thread. like this
fun downloadData() {
Downloader.download(url: "someUrl") { [weak self] in // download is on the background thread
DispatchQueue.main.async { [weak self] in // switch to main
// do some stuff
}
}
}
downloadData()
is currently running on the background thread, the user taps on the home button and the app goes to background and didEnterBackground
is called. While main thread executes code in didEnterBackground
downloadData
finishes downloading and its completion is called and the new task is pushed into main threads queue. So what would happen in this case? Since the code from didEnterBackground
is the last that can be execute before the suspension what would happen to the completion block of downloadData
, would it also be executed before the suspension(after didEnterBackground
) or it would be executed once the user will return to the app, or it will be discarded? Or this situation is not possible at all ? Thank you for your help and again sorry if my question is incorrect.