티스토리 뷰
안녕하세요 Hani입니다.
이번에는 About the UI Restoration Process 아티클을 정리해볼 거예요.
앱이 launch 되는 과정에서 앱의 State Restoration이 일어난다고 하는데
이걸 몰라서!! 🥺 정리할 거예요. ☺️
🚨 이 아티클은 iOS 13.2 까지 적용되는 내용을 담고 있어요. 🚨
앱을 끌 때 상태를 보존하는 것을 State Preservation,
앱을 켤 때 전에 보존된 상태를 복원하는 것을 State Restoration 이라구 합니다.
위 그림은 애플리케이션이 launch되고나서 restore되는 시간까지 메서드가 호출되는 순서를 보여줍니다.
하나씩 알아볼게요 ☺️
🥕 AppDelegate의 willFinishLaunchingWithOptions 메서드를 호출합니다.
이 메서드는 애플리케이션의 launch가 시작되었고,
main 스토리보드나 nib 파일이 메모리에 로드되었으나,
State Restoration이 아직 일어나기 전에 호출됩니다.
(이 메서드가 호출된 시점에 앱의 상태는 Inactive입니다. 😎)
🥕 AppDelegate의 shouldRestoreApplicationState 메서드를 실행합니다.
이 메서드는 앱의 State Restoration이 가능한지 여부를 bool로 반환합니다.
🚨🚨🚨 Deprecated 되었고 iOS 13.2까지 지원합니다.
오직 State Restoration Archive가 사용가능하고,
앱이 State Restoration을 지원해야 다음 과정을 진행할 수 있어요. ☺️
조건을 만족하지 않는다면 앱의 초기화 과정이 종료되어 didFinishLaunchingWithOptions가 실행됩니다. 🥺
우리는 조건을 만족할 때만을 생각하여 State Restoration 과정을 조금 더 볼게욥.
🥕 뷰컨에는 뷰컨을 재생성해주는 restorationClass 인스턴스 프로퍼티가 있는데
restorationClass는 viewControllerWithRestorationIdentifierPath: coder: 타입 메서드를 가지고 있습니다.
restorationClass 타입 프로퍼티에 UIViewControllerRestoration 프로토콜 타입을 할당했다면
UIKit이 해당 메서드를 호출하여 뷰컨을 재생성할 수 있어요. ☺️
var restorationClass: UIViewControllerRestoration.Type? { get set }
static func viewController(withRestorationIdentifierPath identifierComponents: [String], coder: NSCoder) -> UIViewController?
원하는 뷰컨과 조상 뷰컨들의 RestorationIdentifier를 이용하여 뷰컨을 재생성 해달라고 요청할 수 있어요.
nil을 리턴하면 UIKit이 뷰컨을 재생성하는 것을 그만두고 State Restoration 과정이 종료됩니다. 🥺
만약 뷰컨의 restorationClass 프로퍼티에 아무것도 할당하지 않았다면
restorationClass 프로퍼티의 타입 메서드가 아니라
🥕 AppDelegate의 viewControllerWithRestorationIdentifierPath: coder: 인스턴스 메서드가 실행됩니다.
이름이 같죠? ☺️
func application(_ application: UIApplication, viewControllerWithRestorationIdentifierPath identifierComponents: [String], coder: NSCoder) -> UIViewController?
리턴이 nil 일 때는 같은 Restoration Path를 가진 이미 생성된 뷰컨을 탐색하고
그래도 없을 때는 UIKit이 자동적으로 앱의 스토리보드로부터 뷰컨을 인스턴스화시킵니다.
정리하면 뷰컨의 restorationClass 인스턴스 프로퍼티의 타입 메서드로 뷰컨의 재생성을 시도하고
restorationClass가 할당되지 않았으면 AppDelegate의 인스턴스 메서드로 뷰컨의 재생성을 시도하고
nil이 반환되면 Restoration Path로 이미 있는 뷰컨이 있는지 탐색하고
그래도 안되면 마지막으로 스토리보드로 뷰컨을 만들어냅니다. 🧐
휴.. 그럼 생성된 뷰컨을 가지고 어떻게 State Restoration을 진행시키는지 더 알아볼게욥. 😎
🥕 뷰와 뷰컨의 decodeRestorableStateWithCoder가 실행됩니다.
이 메서드는 뷰에도 있고 뷰컨에도 있으며
State Preservation된 정보를 이용하여 뷰 / 뷰컨을 이전 상태로 복원시켜줍니다.
단, 이 메서드는 Override만 해야하고 직접적으로 호출하면 안 되는 메서드예요. 🥺
🥕 AppDelegate의 didDecodeRestorableState를 호출합니다.
이 메서드는 모든 State Restoration 과정이 끝났을 때 호출됩니다. 🥰
드디어 끝났군
🥕 AppDelegate의 didFinishLaunchingWithOptions을 호출합니다.
이 메서드는 State Restoration 과정이 끝난 후, 앱의 Window와 UI가 나타나기 전에 호출됩니다.
이 메서드가 리턴된 후에는 앱의 상태를 Inactive에서 foreground 혹은 background로 이동시키기 위한 메서드를 호출합니다.
UIKit은 State Restoration 과정 이전에 스토리보드와 nib 파일을 load해오기 때문에
만약 코드가 아닌 스토리보드로 뷰컨을 구성했다면
restorationClass / AppDelegate로 뷰컨을 생성하는 것은 좋지 않습니다.
스토리보드를 통해 바로 가져올 수 있으니까요 ☺️
코드로 뷰컨을 재생성할 때는 뷰컨의 restorationIdentifier와 restoreClass를 재할당해야 합니다.
func viewController(withRestorationIdentifierPath
identifierComponents: [Any],
coder: NSCoder) -> UIViewController? {
let vc = MyViewController()
vc.restorationIdentifier = identifierComponents.last as? String
vc.restorationClass = MyViewController.self
return vc
}
References
https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623032-application/
https://developer.apple.com/documentation/uikit/uiviewcontroller/1621472-restorationclass
https://developer.apple.com/documentation/uikit/uiviewcontrollerrestoration/1616859-viewcontroller
https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623006-application
https://developer.apple.com/documentation/uikit/uistaterestoring/1616854-decoderestorablestate
'iOS' 카테고리의 다른 글
[iOS] NSCoder 정리 (0) | 2021.12.07 |
---|---|
[iOS] About the App Launch Sequence 정리 (0) | 2021.11.19 |
[iOS] HTTP 통신 허용하기 (0) | 2021.09.30 |
[iOS] NotificationCenter 정리 (0) | 2021.09.10 |
[iOS] StackView (0) | 2021.05.03 |
- Total
- Today
- Yesterday
- 코딩대회
- CPU와 Memory
- 컴퓨터 추상화
- 강한 순환 참조
- State Restoration
- 에드몬드 카프 알고리즘
- observeOn
- 포드 풀커슨 알고리즘
- WWDC19
- HIG
- WWDC16
- WWDC21
- rxswift
- CompositionalLayout
- 최단경로 문제
- 네트워크 유량
- IOS
- 벨만포드 시간복잡도
- 부스트캠프 6기
- mach-o
- Testable
- 최단경로 알고리즘
- MeTal
- 벨만포드 알고리즘
- 최단경로문제
- 최대 매칭
- test coverage
- 다익스트라 시간복잡도
- 네트워크 플로우
- WWDC17
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |