Extending Your App's Background Execution Time

Panther·2021년 8월 16일
0

https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/extending_your_app_s_background_execution_time

"Ensure that critical tasks finish when your app moves to the background."

앱이 백그라운드로 이동할 때 중요한 작업을 마무리할 수 있도록 합니다.

Overview

앱의 백그라운드 실행 시간을 연장하는 것은 중요한 작업을 수행하기에 시간을 가질 수 있도록 해줍니다. 백그라운드 시간을 요구하는 작업의 경우 Background Tasks를 사용하시기 바랍니다.

Background Tasks
https://developer.apple.com/documentation/backgroundtasks
https://velog.io/@panther222128/Background-Tasks

앱이 백그라운드로 이동하면 시스템은 앱 딜리게이트의 applicationDidEnterBackground(_:) 메소드를 호출합니다. 해당 메소드는 모든 작업 및 반환 수행을 위해 5초의 시간을 갖습니다. 해당 메소드가 반환된 후 잠시 동안 시스템은 앱을 일시정지 상태로 둡니다. 대부분의 앱에서 5초는 중요한 작업 수행에 충분하지만, 더 많은 시간이 필요한 경우 UIKit에게 앱 런타임을 확장하도록 요청할 수 있습니다.

beginBackgroundTask(withName:expirationHandler:) 메소드를 호출해서 앱의 런타임을 확장할 수 있습니다. 이 메소드 호출은 중요한 작업을 수행하기에 추가적인 시간을 줍니다. (backgroundTimeRemaining 속성을 사용해서 사용 가능한 최대 백그라운드 시간을 파악할 수 있습니다.) 작업이 마무리 되면 시스템이 완료되었음을 알 수 있도록 즉시 endBackgroundTask(_:) 메소드를 호출해야 합니다. 적합한 시점에 작업을 완료하지 않으면 시스템은 앱을 종료시킵니다.

Note
앱이 beginBackgroundTask(withName:expirationHandler:) 메소드를 호출하기 위해 백그라운드로의 이동을 기다리지 않으시길 바랍니다. 긴 시간이 필요한 작업 수행 전에 메소드를 호출하시기 바랍니다.

Listing 1은 앱이 데이터를 서버에 저장할 수 있는 작업이면서 5초 이상 걸리는 작업을 백그라운드 작업으로 설정하는 예시를 보여줍니다. beginBackgroundTask(withName:expirationHandler:) 메소드는 endBackgroundTask(_:) 메소드에 저장하고 전달해야 하는 아이덴티파이어를 반환합니다.

Listing 1 Extending the app's background execution time

func sendDataToServer( data : NSData ) {
   // Perform the task on a background queue.
   DispatchQueue.global().async {
      // Request the task assertion and save the ID.
      self.backgroundTaskID = UIApplication.shared.
                 beginBackgroundTask (withName: "Finish Network Tasks") {
         // End the task if time expires.
         UIApplication.shared.endBackgroundTask(self.backgroundTaskID!)
         self.backgroundTaskID = UIBackgroundTaskInvalid
      }
            
      // Send the data synchronously.
      self.sendAppDataToServer( data: data)
            
      // End the task assertion.
      UIApplication.shared.endBackgroundTask(self.backgroundTaskID!)
      self.backgroundTaskID = UIBackgroundTaskInvalid
   }
}

Note
beginBackgroundTask(withName:expirationHandler:) 메소드는 앱 확장으로부터 호출될 수 없습니다. 앱 확장으로부터 추가적인 시간을 요청하려면, ProcessInfoperformExpiringActivity(withReason:using:) 메소드를 대신 호출하시기 바랍니다.

See Also


Background Execution

Updating Your App with Background App Refresh

필요에 따라 백그라운드에서 컨텐트를 가져오고 앱의 인터페이스를 업데이트합니다.

https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/updating_your_app_with_background_app_refresh
https://velog.io/@panther222128/Updating-Your-App-with-Background-App-Refresh

About the Background Execution Sequence

앱이 백그라운드로 이동할 때 커스텀 코드가 수행되는 순서에 대해 알아봅니다.

https://developer.apple.com/documentation/uikit/app_and_environment/scenes/preparing_your_ui_to_run_in_the_background/about_the_background_execution_sequence
https://velog.io/@panther222128/About-the-Background-Execution-Sequence


0개의 댓글