[iOS] App 생명주기 관리하기

HL·2022년 5월 4일
0

iOS

목록 보기
7/22

링크

https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle

공식 문서에 있는 글을 나름대로 정리해보았다


개요

앞쪽 앱은 자원의 우선순위 가짐
뒤쪽 앱은 적은일을 하거나 안해야됨
-> 상태가 바뀌면 동작을 바꿔야함

상태가 바뀔때 UIKit이 메소드 호출하는 메소드

  • iOS 13 이후 버전 -> UISceneDelegate
  • iOS 12 이전 버전 -> UIApplicationDelegate

Scene 기반 생명주기 이벤트 다루기

  • unattached
  • 유저 요청일 경우
    • foreground. 화면
    • UI 닫으면 >> background >> suspanded
  • 시스템 요청일 경우
    • background (ex.위치)
  • background, suspended >> unattached (자원회수)

The following figure shows the state transitions for scenes. When the user or system requests a new scene for your app, UIKit creates it and puts it in the unattached state. User-requested scenes move quickly to the foreground, where they appear onscreen. A system-requested scene typically moves to the background so that it can process an event. For example, the system might launch the scene in the background to process a location event. When the user dismisses your app's UI, UIKit moves the associated scene to the background state and eventually to the suspended state. UIKit can disconnect a background or suspended scene at any time to reclaim its resources, returning that scene to the unattached state.


App 기반 생명주기 이벤트 다루기

  • 앱 실행
  • 화면에 그릴거면
    • inactive
    • 앱을 active 상태
    • background랑 왔다갔다함
  • 화면에 안 그릴거면
    • background

The following figure shows the state transitions involving the app delegate object. After launch, the system puts the app in the inactive or background state, depending on whether the UI is about to appear onscreen. When launching to the foreground, the system transitions the app to the active state automatically. After that, the state fluctuates between active and background until the app terminates.

상태 설명

Foreground

Inactive

실행중이지만 이벤트를 받지는 않음.
Active로 넘어가기 전 거쳐가는 상태
ex) 알림창이 화면을 덮어서 이벤트를 받을 수 없는 상태

Active

앱이 실행중이고 이벤트를 받을 수 있음
Foreground 앱의 일반적인 상태

Background

앱 사용 중 다른 앱을 실행하거나 홈 화면으로 나갔을 때의 상태
백그라운드에서 동작하는 코드를 추가하면 suspended로 넘어가지 않고 유지됨
처음부터 백그라운드 상태로 실행될 경우 inactive가 아닌 background 상태로 진입
ex) 홈으로 나가도 음악 재생

Suspended

앱이 background 상태에서 추가 작업을 하지 않을 경우 바로 진입
메모리에는 올라가 있음
메모리가 부족해질 경우 iOS가 메모리 해제 >> unattached

다른 중요 이벤트 다루기

UIApplicationDelegate 사용하거나
앱의 다른 파트에 notifications해서 처리하기

  • 메모리 경고
  • 잠금될때 데이터
  • Handoff tasks
  • 시간 변할때
  • Open URLs

정리

  • 즉, 12 이전에는 앱 전체가 상태가 왔다갔다 했으면, 13 이후에는 씬별로 왔다갔다 한다는 뜻?
  • 씬이 정확히 뭐지? 하는 생각이 들었다
profile
Frontend 개발자입니다.

0개의 댓글