Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Background execution in iOS

Background execution in iOS

This talk is a brief summary of various way in which iOS applications can run code while being in a background state.

Pawel Rusin

August 13, 2016
Tweet

More Decks by Pawel Rusin

Other Decks in Technology

Transcript

  1. Pawel Rusin (@RusinPaw) iOS dev at Cookpad Tokyo iOS Meetup

    2016-08-13 Background execution in iOS
  2. Agenda • Motivation • App lifecycle • Background execution modes

    – App leaves the foreground – App enters the background – App enters the foreground • Summary
  3. Executing Finite-Length Tasks When the user is not actively using

    your app, the system moves it to the background state. For many apps, the background state is just a brief stop on the way to the app being suspended. Suspending apps is a way of improving battery life it also allows the system to devote important system resources to the new foreground app that has drawn the user’s attention. An app in the background state should do as little work as possible. Apps that request time to process specific types of events should process those events and return control back to the system as quickly as possible. App Programming Guide for iOS (background execution) UIAppDelegate documentation
  4. Executing Finite-Length Tasks When the user is not actively using

    your app, the system moves it to the background state. For many apps, the background state is just a brief stop on the way to the app being suspended. Suspending apps is a way of improving battery life it also allows the system to devote important system resources to the new foreground app that has drawn the user’s attention. An app in the background state should do as little work as possible. Apps that request time to process specific types of events should process those events and return control back to the system as quickly as possible. App Programming Guide for iOS (background execution) UIAppDelegate documentation
  5. • Passes the download task to a separate process •

    Requires specific type of NSURLSession – background session • Gwendolyn Weston: “While Your App Was Sleeping: Background Transfer Services” Downloading Content in the Background
  6. Audio and AirPlay Location updates Voice over IP Newsstand downloads

    External accessory communication Uses Bluetooth LE accessories Acts as a Bluetooth LE accessory Background fetch Remote notifications launch App enters the background
  7. App enters the background trigger Audio buffer empty Location update

    Download task finished Time elapsed Silent push data from a peripheral
  8. Audio and AirPlay Location updates Voice over IP Newsstand downloads

    External accessory communication Uses Bluetooth LE accessories Acts as a Bluetooth LE accessory Background fetch Remote notifications launch App enters the background
  9. Audio and AirPlay Location updates Voice over IP Newsstand downloads

    External accessory communication Uses Bluetooth LE accessories Acts as a Bluetooth LE accessory Background fetch Remote notifications launch App enters the background
  10. • Apps that need to check for new content periodically

    can ask the system to wake them up so that they can initiate a fetch operation for that content. • Enabling this mode is not a guarantee that the system will give your app any time to perform background fetches. • The system must balance your app’s need to fetch content with the needs of other apps and the system itself. Background fetch https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgramming Guide/BackgroundExecution/BackgroundExecution.html
  11. Background fetch func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)

    { //fetch sth if error != nil { return completion(UIBackgroundFetchResultFailed) } else if newData { return completion(UIBackgroundFetchResultNewData) } else { return completion(UIBackgroundFetchResultNoData) } }
  12. • Specific tasks of a short execution time • Know

    your API (and your limitations) • Don't be greedy App enters the background
  13. App enters the foreground let localNotification = UILocalNotification() localNotification.fireDate =

    NSDate(timeIntervalSinceNow: 300) // 5mins localNotification.alertBody = "stand up!" localNotification.timeZone = NSTimeZone.defaultTimeZone() UIApplication.sharedApplication().scheduleLocalNotification(l ocalNotification)
  14. • One way of “reminding” user about the app •

    Alarms, motivational apps • Be nice! App enters the foreground
  15. • What can be done? • What cannot be done?

    • With great power comes great responsibility Summary