iOS 后台传输使用 NSURLSession

标签 ios objective-c

我对背景有一些疑问。正如我在理论上读到的那样,一旦所有下载都已下载,就会调用 handleEventsForBackgroundURLSession。这不是iOS触发的,还是应用端触发的。此外,每次下载完成后,我会阅读 URLSessionDidFinishEventsForBackgroundURLSession 如果应用程序由于应用程序崩溃或任何其他原因未运行或处于挂起状态,将调用该应用程序是否在内存中。我的假设是,如果应用程序处于挂起状态,则 URLSessionDidFinishEventsForBackgroundURLSession 已实现的下载 Controller 类也不会在内存中。那么谁能解释一下这个后台操作是如何在操作系统级别发生的?我还读到,一旦下载完所有内容,该应用程序就会唤醒。也可以解释一下吗。。

提前致谢。

最佳答案

根据 Apple 的 documentation :

If an iOS app is terminated by the system and relaunched, the app can use the same identifier to create a new configuration object and session and retrieve the status of transfers that were in progress at the time of termination. This behavior applies only for normal termination of the app by the system. If the user terminates the app from the multitasking screen, the system cancels all of the session’s background transfers. In addition, the system does not automatically relaunch apps that were force quit by the user. The user must explicitly relaunch the app before transfers can begin again.

因此,如果应用程序没有被用户终止,但应用程序由于某种原因被终止(例如,如果 iOS 终止它以释放内存),系统将在所有任务完成后唤醒您的应用程序,调用完成处理程序。您的 NSURLSession 后台任务(下载和上传)在操作系统守护进程中运行,而不是在应用程序进程中运行,因此即使应用程序被 iOS 终止,它们也会继续运行。

关于iOS 后台传输使用 NSURLSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33852545/

相关文章:

iphone - 如何使用 UITextRange 从 UITextField 获取子字符串

ios - Xcode 8.1 IOS10.1 CoreData删除并没有真正删除

iphone - NSData dataWithContentsOfURL : stopped working in iOS 6

objective-c - 在 Objective-C 中创建私有(private)属性

objective-c - Objective c 类扩展 header - xcode 中的新功能

ios - NSMutableAttributedString 的属性 NSStrikethroughStyleAttributeName 在 iOS8 中不能正常工作

iphone - 如何通过 UISlider 更新 iPhone 上的音乐音量?

ios - 如何在关闭任务完成后删除监听器?

ios - 比较 Swift 中字典的相等性

iOS:NSClassFromString 为内部类返回 nil