iOS, Swift/Tip, Bug, Swift Error
Swift ) 앱 종료 감지, 지연 applicationWillTerminate - EEYatHo iOS
EEYatHo
2023. 6. 30. 15:30
applicationWillTerminate
- UIKit > UIApplication.h > applicationWillTerminate 을 가지고 앱 종료 직전을 컨트롤할 수 있음.
- 해당 메소드가 return 되면, 시스템이 앱을 종료한다.
앱 종료 지연 및 async 작업
- applicationWillTerminate 메소드에 sync 코드를 넣으면 다 실행하고 종료되지만,
NotificationCenter, API 호출 등의 async 작업은, 미처 실행되기 전에 함수가 종료되서,
동작하지 않는 경우가 발생한다. - 이를 방지하기 위해서는 return 을 지연(sleep) 시키면 된다.
- 문서에 따르면 최대 약 5초까지 지연시킬 수 있다.
코드
/// 해당 함수가 반환되면 시스템이 앱을 즉시 종료.
/// 최대 5초까지 지연할 수 있음 ( API 통신 등을 위한 시간 확보 )
/// 참고 링크 : https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623111-applicationwillterminate
func applicationWillTerminate(_ application: UIApplication) {
print("\(type(of: self)): \(#function)")
RxBus.shared.post(event: Events.ApplicationWillTerminateEvent())
sleep(5)
print("💣💣💣 applicationWillTerminate return")
}
Reference
반응형