iOS App Programming Guide => App States

本文介绍了iOS应用程序的各种状态,包括未运行、非活动、活跃、后台和暂停,并详细阐述了如何管理和响应这些状态变化。此外,还讲解了启动周期、中断处理、返回前台的过程以及与后台执行和多任务处理相关的重要概念。

iOS App Programming Guide

App States and Multitasking

Managing App State Changes

App states

State

Description

Not running

The app has not been launched or was running but was terminated by the system.

Inactive

The app is running in the foreground but is currently not receiving events. (It may be executing other code though.) An app usually stays in this state only briefly as it transitions to a different state.

Active

The app is running in the foreground and is receiving events. This is the normal mode for foreground apps.

Background

The app is in the background and executing code. Most apps enter this state briefly on their way to being suspended. However, an app that requests extra execution time may remain in this state for a period of time. In addition, an app being launched directly into the background enters this state instead of the inactive state. 

Suspended

The app is in the background but is not executing code. The system moves apps to this state automatically and does not notify them before doing so. While suspended, an app remains in memory but does not execute any code.

When a low-memory condition occurs, the system may purge suspended apps without notice to make more space for the foreground app.


Most state transitions are accompanied by a corresponding call to the methods of your appdelegate object(e.g.application:willFinishLaunchingWithOptions: ).

The App Launch Cycle

As part of the launch cycle, the system creates a process and main thread for your app and calls your app’s main function on that main thread.

Application life cycle

If your app is launched into the background, the main difference is that instead of your app being made active, it enters the background state to handle the event and then is suspended shortly afterward:

To determine whether your app is launching into the foreground or background, check theapplicationState property of the shared UIApplication object.

About the main Function

#import <UIKit/UIKit.h>

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([MyAppDelegate class]));
    }
}
UIApplicationMain parameters:

  • The third parameter identifies the name of the principal app class. This is the class responsible for running the app. It is recommend that you specifynil for this parameter, which causes UIKit to use theUIApplication class.

  • The fourth parameter identifies the class of your custom app delegate.

UIApplication is also responsible to load the app's main user interface file(The storyboard file or nib file).

What to Do at Launch Time
  • Check the contents of the launch options dictionary for information about why the app was launched, and respond appropriately.

  • Initialize the app’s critical data structures.

  • Prepare your app’s window and views for display.

    • If your app does not automatically load a main storyboard or nib file at launch time, you can use theapplication:willFinishLaunchingWithOptions: method to prepare your app’s window for display.

pps are expected to launch and initialize themselves and start handling events in less than 5 seconds. If an app does not finish its launch cycle in a timely manner, the system kills it for being unresponsive.

Responding to Interruptions

Moving to the Background


What to Do When Moving to the Background
  • Prepare to have their picture taken. When the applicationDidEnterBackground: method returns, the system takes a picture of your app’s user interface and uses the resulting image for transition animations. If any views in your interface contain sensitive information, you should hide or modify those views before theapplicationDidEnterBackground: method returns.

  • Save user data and app state information. All unsaved changes should be written to disk when entering the background.

  • Free up as much memory as possible.

Your app delegate’s applicationDidEnterBackground: method has approximately 5 seconds to finish any tasks and return. If the method does not return before time runs out, your app is killed and purged from memory.

Memory Usage for Background Apps
Apps that consume large amounts of memory while in the background are the first apps to be terminated.

Returning to the Foreground

Processing Queued Notifications at Wakeup Time
A suspended app does not execute any code and therefore cannot process notifications related to orientation changes, time changes, preferences changes, and many others that would affect the app’s appearance or state. To prevent your app from becoming overloaded with notifications when it resumes, the system coalesces events and delivers a single notification (of each relevant type) that reflects the net change since your app was suspended.

Notifications delivered to waking apps    =>    Table 3-2

App Termination

If your app is running (either in the foreground or background) at termination time, the system calls your app delegate’sapplicationWillTerminate: method so that you can perform any required cleanup.

The applicationWillTerminate: method is not called if your app is currently suspended.

The Main Run Loop

The main run loop of your app is responsible for processing all user-related events. The UIApplication object sets up the main run loop at launch time and uses it to process events and handle updates to view-based interfaces. As the name suggests, the main run loop executes on the app’s main thread. This behavior ensures that user-related events are processed serially in the order in which they were received.

As the user interacts with a device, events related to those interactions are generated by the system and delivered to the app via a special port set up by UIKit. Events are queued internally by the app and dispatched one-by-one to the main run loop for execution. The UIApplication object is the first object to receive the event and make the decision about what needs to be done. A touch event is usually dispatched to the main window object, which in turn dispatches it to the view in which the touch occurred:

Processing events in the main run loop

Common types of events for iOS apps   ---   Table 3-3       (Event Handling Guide for iOS)

Background Execution and Multitasking

If you find you need to perform background tasks, here are some guidelines for when that is appropriate:

  • You need to implement at least one of several specific user services.

  • You need to perform a single finite-length task.

  • You need to use notifications to alert the user to some relevant piece of information when your app is not running.

Background modes for apps   ---   Table 3-4

Concurrency and Secondary Threads

The system creates your app’s main thread but your app can create additional threads, as needed, to perform other tasks. The preferred way to create threads is to let the system do it for you by using Grand Central Dispatch queues and operation queues. Both types of queue provide an asynchronous execution model for tasks that you define.

Concurrency Programming Guide.

随着全民健身事业的深入推进与户外运动的快速普及,定向越野赛事举办频次持续提升,赛事规模与参与人数不断增长,参与者与组织者对赛事组织效率、服务质量及管理规范化的要求日益提高。然而,传统定向越野赛事管理仍依赖人工登记、线下核对、纸质记录等方式,普遍存在信息同步滞后、流程繁琐易错、数据统计低效、成绩核算耗时、资金与签到管理不规范等突出问题。例如,人工报名信息核对易出现遗漏与错误,现场签到排队拥堵影响参赛体验,成绩人工录入误差率高,赛事资金与物资管理缺乏透明化监管。这些问题不仅大幅增加赛事组织成本与人力消耗,还制约赛事运营效率与整体服务水平提升。在此背景下,构建一套数字化、一体化的定向越野赛事管理系统,成为赛事运营主体优化管理模式、提升服务质量的迫切需求。本研究旨在通过信息化技术重构赛事管理全流程,解决传统模式下的信息孤岛与操作低效问题,为定向越野赛事规范化、智能化管理提供可落地的解决方案。 本研究基于 Spring Boot 与 Vue 技术栈,采用前后端分离架构设计并实现了一套定向越野赛事管理系统。技术层面:后端依托 Spring Boot 框架搭建 RESTful API 服务,利用其自动配置与模块化特性简化开发流程,集成 MyBatis-Plus 优化数据持久化操作;前端采用 Vue.js 框架实现组件化开发,通过 Element UI 组件库构建交互友好的可视化界面,利用 Axios 实现前后端数据动态交互;数据库选用 MySQL 保障数据高效存储与事务一致性,同时采用手机号短信验证、JWT 令牌等机制强化系统安全性与用户权限管理。 本系统的实施为定向越野赛事运营与管理提供了显著的现实价值:其一,通过线上报名、信息筛选与自动化核对,大幅降低人工操作误差,提升赛事组织效率 30% 以上;其二,定位打卡签到与实时成绩同步功能,实现参赛流程无纸化、智能化,显著改善参赛者体验;其
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值