调用didEnterBackground之后是否可以在主线程上执行某些操作? [英] Is it possible to execute something on main thread after didEnterBackground is called?

查看:162
本文介绍了调用didEnterBackground之后是否可以在主线程上执行某些操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,如果您认为我的问题是虚拟的,我想对不起,我是iOS和多线程的新手,只是想了解事情的发展.据我所知,didEnterBackground是iOS在应用程序暂停之前调用的最后一个函数,并且该应用程序需要大约5秒钟才能从应用程序中返回,否则iOS会终止该应用程序.我目前正在考虑这种情况-我的某些任务正在后台线程上发生(例如网络下载),并且其完成块发生在主线程上.像这样

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()当前在后台线程上运行,用户点击主屏幕按钮,应用程序进入后台,并调用didEnterBackground.当主线程在didEnterBackground downloadData中执行代码时,下载完成并被调用,并将新任务推入主线程队列.那么在这种情况下会发生什么呢?由于来自didEnterBackground的代码是在暂停之前可以执行的最后一个代码,因此downloadData的完成块会发生什么,因此它也将在暂停之前执行(在didEnterBackground之后),或者一旦执行用户将返回到应用程序,还是将其丢弃?还是根本不可能出现这种情况?谢谢您的帮助,如果我的问题不正确,再次表示歉意.

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.

推荐答案

首先,我建议您考虑提供本文档

First of all, I suggest you consider this document provided Apple. The document is clear by itself, but I'll go through it to extract important information that matches your question.

在此文档中,苹果明确表示所有已挂起的UI任务都将被丢弃,并且您不应将新任务提交到调度队列,并且您应挂起当前队列并释放资源尽快.

In this document, apple says explicitly that every suspended UI task will be discarded and you should not submit new tasks to Dispatch queues and you should suspend the current queues and release resources ASAP.

应该停用应用程序时,分两个步骤:

When app is supposed to be deactivated, There are two steps:

  1. 系统调用应用程序applicationWillResignActive(_:)方法(如果您使用的是sceneDelegate,则调用sceneWillResignActive(_:))
  2. 然后UIKit调用applicationDidEnterBackground(_:)方法(或sceneDidEnterBackground(_:))
  1. System calls application applicationWillResignActive(_:) method (or sceneWillResignActive(_:) If your using sceneDelegate)
  2. Then UIKit calls applicationDidEnterBackground(_:) method (or sceneDidEnterBackground(_:))

第一步,UIKit会通知您有关停用的信息,这意味着在过了几次之后,该应用程序将进入后台. 在执行此步骤时,必须保存所有敏感数据.

In the first step, UIKit is notifying you about deactivation, which implies that after a few times later, the app will enter in background. While this step, you must save all the sensitive data.

在此步骤中,如果您已暂停UI任务,则所有这些任务都将被丢弃.

During this step, if you have suspended UI tasks, All of them will be discarded.

当需要临时中断应用程序(例如,显示系统警报)时,系统还会停用其应用程序

The system also deactivates apps when it needs to interrupt them temporarily—for example, to display system alerts

在此步骤中,您必须暂停正在运行的任务,正如Apple所说的那样,您应该使您的应用保持安静状态

During this step, you must pause your running task, As Apple says, You should make your app quiet

  • 将用户数据保存到磁盘并关闭所有打开的文件.

  • Save user data to disk and close any open files.

挂起调度和操作队列.

不要安排任何新任务来执行.

Don’t schedule any new tasks for execution.

使所有活动计时器无效.

Invalidate any active timers.

自动暂停游戏.

...

在UIKit通知您的应用有关转移tp后台状态的第二步中,您必须尽快释放共享资源.如果您继续使用这些资源,则您的应用将被终止.

In the Second steps when UIKit notifies your app about transferring tp background state, You must free shared resources as quickly as possible, If you persist to use the resources your app will be terminated.

因此,您问题中提到的UI任务将被丢弃.

我还建议研究此文档有关应用状态的信息,有助于理解应用状态.

I also recommend studying this document about app states which helps with understanding application state.

这篇关于调用didEnterBackground之后是否可以在主线程上执行某些操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆