程序转到后台与applicationWillTerminate不响应

最近对数据进行归档处理,却发现按home键之后,applicationWillTerminate并不响应,

查了一下,发现iOS 4.0之后,按home键已经是处理applicationDidEnterBackground了。

相关文档如下:

Moving to the Background

When the user presses the Home button or the system launches another app, the foreground app transitions
to the inactive state and then to the background state. These transitions result in calls to the app delegate’s
applicationWillResignActive: and applicationDidEnterBackground: methods, as shown in
Figure 3-5. After returning from the applicationDidEnterBackground: method, most apps move to the
suspended state shortly afterward. Apps that request specific background tasks (such as playing music) or
that request a little extra execution time from the system may continue to run for a while longer.

Your app delegate’s applicationDidEnterBackground: method has approximately 5 seconds to finish
any tasks and return. In practice, this method should return as quickly as possible. If the method does not
return before time runs out, your app is killed and purged from memory. If you still need more time to perform
tasks, call the beginBackgroundTaskWithExpirationHandler: method to request background execution
time and then start any long-running tasks in a secondary thread. Regardless of whether you start any

background tasks, the applicationDidEnterBackground: method must still exit within 5 seconds.


程序处理applicationDidEnterBackground之后,系统会让程序有5秒钟的时间来保留数据,对于一般的数据存储,5秒足够了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
macOS开发中,App的生命周期是指App从启动到关闭的整个过程。主要包括以下几个阶段: 1. 启动阶段:当用户单击或双击App的图标时,系统会检测并初始化App的相关资源,包括加载App的代码和资源文件等。同时,系统会调用App的代理对象的一些方法,比如`applicationDidFinishLaunching`方法,开发者可以在此方法中进行一些初始化操作,如注册通知、设置全局参数等。 2. 运行阶段:App启动后,用户可以与其进行交互。App会响应用户的操作事件,如点击按钮、滑动屏幕等。在运行阶段,App会接收来自系统的事件消息,并做出相应的处理。同时,App还可以主动发送消息给其他对象,实现不同模块之间的通信和交互。 3. 后台和挂起阶段:当用户将App切换到后台时,App进入后台执行状态,可以继续执行一些任务,比如下载、数据上传等。在后台状态下,App可以接收远程推送通知,并执行相关操作,如更新内容、显示提醒等。当系统资源紧张时,会将App挂起,此时App会进入挂起状态,停止执行,释放资源,以便系统能够优先处理其他任务。 4. 退出阶段:当用户关闭App时,系统会调用App代理对象的`applicationWillTerminate`方法,开发者可以在此方法中进行一些清理操作,如保存数据、关闭文件等。之后,系统会释放App的相关资源,关闭App进程。 总结:App的生命周期包括启动、运行、挂起和退出等阶段。开发者可以通过合理地实现代理方法和事件处理,控制和管理App的运行流程,提高用户体验和性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值