是否总是在applicationWillTerminate之前调用applicationDidEnterBackground? [英] Is applicationDidEnterBackground ALWAYS called before applicationWillTerminate?

查看:123
本文介绍了是否总是在applicationWillTerminate之前调用applicationDidEnterBackground?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS应用中,总是在applicationWillTerminate之前调用applicationDidEnterBackground吗?我知道applicationWillTerminate并不总是被调用(多任务处理)-但是当它被调用时,applicationDidEnterBackground总是被首先调用吗?对于支持多任务的应用程序,我不想不必要地通过将代码包含在applicationWillTerminate中来将其复制到applicationWillTerminate中.

Is applicationDidEnterBackground ALWAYS called before applicationWillTerminate in an iOS app? I know that applicationWillTerminate is not always called (multitasking) - but when it is called, is applicationDidEnterBackground ALWAYS called first? I don't want to duplicate code unnecessarily by including it in applicationWillTerminate if it is already included in applicationDidEnterBackground, for an app that supports multitasking.

推荐答案

被调用,而不是applicationWillTerminate而不是applicationWillTerminate,因此您不必同时调用它们.这是Apple文档的一部分:

in ios 4.0 and later applicationDidEnterBackground is called instead of applicationWillTerminate so you don't have to call both of them. Here is the portion of the Apple docs:

讨论

在iOS 4.0及更高版本中,将调用此方法,而不是 applicationWillTerminate:用户退出应用程序时的方法 支持后台执行.您应该使用此方法 释放共享资源,保存用户数据,使计时器无效并存储 足够的应用程序状态信息可将您的应用程序还原到 它的当前状态,以防稍后被终止.你也应该 禁用对应用程序用户界面的更新,并避免使用 某些类型的共享系统资源(例如用户的联系人 数据库).还必须避免在 背景.

In iOS 4.0 and later, this method is called instead of the applicationWillTerminate: method when the user quits an application that supports background execution. You should use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. You should also disable updates to your application’s user interface and avoid using some types of shared system resources (such as the user’s contacts database). It is also imperative that you avoid using OpenGL ES in the background.

您实现此方法大约需要五秒钟 执行任何任务并返回.如果您需要更多时间来执行 任何最终任务,您都可以从 通过调用beginBackgroundTaskWithExpirationHandler:系统.在 实践中,您应该从applicationDidEnterBackground:返回 尽快.如果该方法在时间运行之前未返回 终止您的应用程序并将其从内存中清除.

Your implementation of this method has approximately five seconds to perform any tasks and return. If you need additional time to perform any final tasks, you can request additional execution time from the system by calling beginBackgroundTaskWithExpirationHandler:. In practice, you should return from applicationDidEnterBackground: as quickly as possible. If the method does not return before time runs out your application is terminated and purged from memory.

您应该执行与调整用户界面有关的所有任务 在此方法退出之前,但其他任务(例如保存状态)应 根据需要将其移动到并发调度队列或辅助线程. 因为可能是您从中开始的任何后台任务 applicationDidEnterBackground:直到该方法之后才运行 退出,您应该在请求额外的后台执行时间之前 开始这些任务.换句话说,第一个电话 beginBackgroundTaskWithExpirationHandler :,然后在 分派队列或辅助线程.

You should perform any tasks relating to adjusting your user interface before this method exits but other tasks (such as saving state) should be moved to a concurrent dispatch queue or secondary thread as needed. Because it's likely any background tasks you start in applicationDidEnterBackground: will not run until after that method exits, you should request additional background execution time before starting those tasks. In other words, first call beginBackgroundTaskWithExpirationHandler: and then run the task on a dispatch queue or secondary thread.

应用程序还会发布一个 UIApplicationDidEnterBackgroundNotification通知围绕 同时调用此方法使感兴趣的对象有机会 应对过渡.

The application also posts a UIApplicationDidEnterBackgroundNotification notification around the same time it calls this method to give interested objects a chance to respond to the transition.

有关如何正常过渡到 后台,以及有关如何在以下位置启动后台任务的信息 退出时间,请参阅iOS App编程指南.

For more information about how to transition gracefully to the background, and for information about how to start background tasks at quit time, see iOS App Programming Guide.

希望这可以帮助您解决此问题. 阿德里安

Hope this helps clear the issue for you man. Adrian

此处是开发人员部分上可用的技术说明的链接.它正在处理网络和多任务处理.本文档中使用的实际方法仅处理applicationDidEnterBackground,并且自iOS 5起,它们具有名为 watchdog 的系统,如果网络无法自动响应,该系统将终止应用程序.因此,无需实际调用applicationWillTerminate并尝试执行代码以允许您的应用在终止应用之前完成其任务.该应用程序将进入后台,并将继续其任务,直到完成最后一个任务.我希望这是有道理的,但这是链接.请阅读看门狗部分. https://developer. apple.com/library/ios/#technotes/tn2277/_index.html#//apple_ref/doc/uid/DTS40010841

Here is the link to the technical note that is available on developer section. It is dealing with networking and multitasking. The actual method used in this doc deals with only applicationDidEnterBackground and since iOS 5 they have a system called watchdog which terminates the app if the network is unresponsive automatically. Hence there is no need to actually call applicationWillTerminate and try to execute codes to allow your app to finish its task before the app is terminated. The app will enter the background and will continue its task until the last task is completed. I hope that makes sense, but here is the link. Please read the watchdog section. https://developer.apple.com/library/ios/#technotes/tn2277/_index.html#//apple_ref/doc/uid/DTS40010841

希望这会有所帮助. :)

Hope this helps. :)

这篇关于是否总是在applicationWillTerminate之前调用applicationDidEnterBackground?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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