Multitasking in iOS - The Junction

80
| The Junction Sunday, July 31, 2011

description

iOS advanced engineering session focused on multitasking presented at The Junction

Transcript of Multitasking in iOS - The Junction

Page 1: Multitasking in iOS - The Junction

| The Junction

Sunday, July 31, 2011

Page 2: Multitasking in iOS - The Junction

| The Junction 2

Multitasking in iOSThe Junction

Ori GoshenCo-Founder & Chief Architectmail: [email protected]: @origoshen

Sunday, July 31, 2011

Page 3: Multitasking in iOS - The Junction

| The Junction 3

OUTLINE

• Introduction

• Multitasking In iOS Overview

• Application Life Cycle

• Best Practices

• Multitasking & Development Tools

Sunday, July 31, 2011

Page 4: Multitasking in iOS - The Junction

| The Junction 4

Introduction

Sunday, July 31, 2011

Page 5: Multitasking in iOS - The Junction

| The Junction 5

ME

• Coder since birth

• IDF (8200) - Project manager & dev. team leader

• Fring - Product & development team leader

• Tawkon - Co-Founder & Chief Architect

• Love & hate relationship with

Sunday, July 31, 2011

Page 6: Multitasking in iOS - The Junction

| The Junction 6

ABOUT TAWKON

Drive Safely Eat Wisely Talk Responsibly

Sunday, July 31, 2011

Page 7: Multitasking in iOS - The Junction

| The Junction 7

ABOUT TAWKON

Sunday, July 31, 2011

Page 8: Multitasking in iOS - The Junction

| The Junction 8

HOW APPLE THINKS?

Sunday, July 31, 2011

Page 9: Multitasking in iOS - The Junction

| The Junction 9

MIND & SPIRIT

• Watch Apple Engineering Video Tutorials

• Watch WWDC Technical Sessions & Keynote

• Read The Guidelines! It’s NOT A Waste Of Time

• Read Coding Documentation

• Follow Apple Evangelists On Twitter

• Follow @gruber

• Go to macrumors.com

• Go to tuaw.com (how I found my iPhone)

Sunday, July 31, 2011

Page 10: Multitasking in iOS - The Junction

| The Junction 10

Multitasking In iOS Overview

Sunday, July 31, 2011

Page 11: Multitasking in iOS - The Junction

| The Junction 11

INTRODUCTION

• A major feature introduced in iOS 4

• Improves user experience

• “Every app should adopt multitasking”

• Supported on all iOS Devices

• Except old ones (iPhone, iPhone 3G, 1st gen. touch)

• Android vs. iPhone Multitasking

• Multitasking Android Campaign

Sunday, July 31, 2011

Page 12: Multitasking in iOS - The Junction

| The Junction 12

iOS MULTITASKING PHILOSOPHY

• “General purpose concurrency is not the solution for

mobile devices”

Background execution:finish task, audio, location, VoIP

Push Notification

Fast AppSwitching

Sunday, July 31, 2011

Page 13: Multitasking in iOS - The Junction

| The Junction 13

THE SERVICES

• Fast App Switching

• Resume quickly, preserve state

• Push Notifications

• Respond to a notification sent from a remote server

• Local Notifications

• Push-style notification delivered at a predetermined time

• NewsStand Push Notification (New on iOS 5)

Sunday, July 31, 2011

Page 14: Multitasking in iOS - The Junction

| The Junction 14

THE SERVICES

• Background audio

• Play audible content to the user while in the background

• Task completion

• Extra time to complete a task

• Location-Navigation

• Keep users continuously informed of their location

• Location-Significant location change, region monitoring

• Respond to location changes while in the background

• Voice over IP

• Make and receive phone calls using an Internet connection

Sunday, July 31, 2011

Page 15: Multitasking in iOS - The Junction

| The Junction 15

Sunday, July 31, 2011

Page 16: Multitasking in iOS - The Junction

| The Junction 16

DO YOU REALLY NEED TO MULTITASK?

Sunday, July 31, 2011

Page 17: Multitasking in iOS - The Junction

| The Junction 17

WHAT IF... I’M DOING SOMETHING ELSE?

• Triple check if this is something that can be done remotely

• Server side push notifications

• Daemons (Jailbroken Option)

• Mmmm....What about a clipboard history app?

• Piggyback on available scenario and think of a damn good

excuse (you gonna need it!)

Sunday, July 31, 2011

Page 18: Multitasking in iOS - The Junction

| The Junction 18

FAST APP SWITCHING

• App Resumes Immediately

• App State Is Preserved

• Tight integration with multitasking UI

• Default for all apps being developed on SDK 4+

• In other words: “processes are being suspended”

Sunday, July 31, 2011

Page 19: Multitasking in iOS - The Junction

| The Junction 19

TASK COMPLETION

• Application can complete a task without remaining in

the foreground

• User does not have to wait for the task to complete

• Task duration is limited to avoid excessive battery

drain

• Examples:

• Uploading photos or videos

• Finishes applying an image filter

• Finishes downloading a magazine

Sunday, July 31, 2011

Page 20: Multitasking in iOS - The Junction

| The Junction 20

BACKGROUND AUDIO

• Plays audible content to the user

• Streams audio

• Record audio

• Continue playing/recording in the background

• Integrates with remote controls

Sunday, July 31, 2011

Page 21: Multitasking in iOS - The Junction

| The Junction 21

LOCATION NAVIGATION

• Keep users informed of their location in the

background

• Gives turn-by-turn directions

Sunday, July 31, 2011

Page 22: Multitasking in iOS - The Junction

| The Junction 22

LOCATION TRACKING

• Respond to location changes while in the

background

• Suitable for location aware apps

• Significant location changes

• Sends notification after changing cell towers

• Region monitoring

• Sends a notification upon entering and

exiting regions of interest

• Not supported on old iPhone (3G, 3GS, ..)

Sunday, July 31, 2011

Page 23: Multitasking in iOS - The Junction

| The Junction 23

LOCATION TRACKING

Significant location changes

Location changed

Location changed

Location changed

57

Significant location changes

Sunday, July 31, 2011

Page 24: Multitasking in iOS - The Junction

| The Junction 24

LOCATION TRACKINGRegion monitoring

Region monitoring

Entered region

62

Sunday, July 31, 2011

Page 25: Multitasking in iOS - The Junction

| The Junction 25

VOIP

• Helps you maintain the connection with the VOIP server

• Keep alive with the server (min. of 10 mins.)

• Maintain a signaling connection

• Notifying the user on incoming call

Sunday, July 31, 2011

Page 26: Multitasking in iOS - The Junction

| The Junction 26

Application Life Cycle

Sunday, July 31, 2011

Page 27: Multitasking in iOS - The Junction

| The Junction 27

APPLICATION LIFE CYCLELaunch and active/inactive

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

Sunday, July 31, 2011

Page 28: Multitasking in iOS - The Junction

| The Junction 27

APPLICATION LIFE CYCLELaunch and active/inactive

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

Sunday, July 31, 2011

Page 29: Multitasking in iOS - The Junction

| The Junction 27

APPLICATION LIFE CYCLELaunch and active/inactive

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

applicationDidFinishLaunchingWithOptions:

Sunday, July 31, 2011

Page 30: Multitasking in iOS - The Junction

| The Junction 27

APPLICATION LIFE CYCLELaunch and active/inactive

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

applicationDidFinishLaunchingWithOptions:

Sunday, July 31, 2011

Page 31: Multitasking in iOS - The Junction

| The Junction 27

APPLICATION LIFE CYCLELaunch and active/inactive

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

applicationDidFinishLaunchingWithOptions:

applicationDidBecomeActive:

Sunday, July 31, 2011

Page 32: Multitasking in iOS - The Junction

| The Junction 27

APPLICATION LIFE CYCLELaunch and active/inactive

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

applicationDidFinishLaunchingWithOptions:

applicationDidBecomeActive:

Sunday, July 31, 2011

Page 33: Multitasking in iOS - The Junction

| The Junction 27

APPLICATION LIFE CYCLELaunch and active/inactive

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

applicationDidFinishLaunchingWithOptions:

applicationWillResignActive:

applicationDidBecomeActive:

Sunday, July 31, 2011

Page 34: Multitasking in iOS - The Junction

| The Junction 28

APPLICATION LIFE CYCLESwitch from an app

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

Sunday, July 31, 2011

Page 35: Multitasking in iOS - The Junction

| The Junction 28

APPLICATION LIFE CYCLESwitch from an app

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

Sunday, July 31, 2011

Page 36: Multitasking in iOS - The Junction

| The Junction 28

APPLICATION LIFE CYCLESwitch from an app

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

applicationWillResignActive:

Sunday, July 31, 2011

Page 37: Multitasking in iOS - The Junction

| The Junction 28

APPLICATION LIFE CYCLESwitch from an app

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

applicationWillResignActive:

Sunday, July 31, 2011

Page 38: Multitasking in iOS - The Junction

| The Junction 28

APPLICATION LIFE CYCLESwitch from an app

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

applicationWillResignActive:

applicationDidEnterBackground:

Sunday, July 31, 2011

Page 39: Multitasking in iOS - The Junction

| The Junction 28

APPLICATION LIFE CYCLESwitch from an app

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

applicationWillResignActive:

applicationDidEnterBackground:

Sunday, July 31, 2011

Page 40: Multitasking in iOS - The Junction

| The Junction 28

APPLICATION LIFE CYCLESwitch from an app

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

applicationWillResignActive:

applicationDidEnterBackground:

Sunday, July 31, 2011

Page 41: Multitasking in iOS - The Junction

| The Junction 29

APPLICATION LIFE CYCLESwitch to an app

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

Sunday, July 31, 2011

Page 42: Multitasking in iOS - The Junction

| The Junction 29

APPLICATION LIFE CYCLESwitch to an app

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

Sunday, July 31, 2011

Page 43: Multitasking in iOS - The Junction

| The Junction 29

APPLICATION LIFE CYCLESwitch to an app

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

Sunday, July 31, 2011

Page 44: Multitasking in iOS - The Junction

| The Junction 29

APPLICATION LIFE CYCLESwitch to an app

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

Sunday, July 31, 2011

Page 45: Multitasking in iOS - The Junction

| The Junction 29

APPLICATION LIFE CYCLESwitch to an app

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

applicationDidEnterForeground:

Sunday, July 31, 2011

Page 46: Multitasking in iOS - The Junction

| The Junction 29

APPLICATION LIFE CYCLESwitch to an app

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

applicationDidEnterForeground:

Sunday, July 31, 2011

Page 47: Multitasking in iOS - The Junction

| The Junction 29

APPLICATION LIFE CYCLESwitch to an app

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

applicationDidBecomeActive:

applicationDidEnterForeground:

Sunday, July 31, 2011

Page 48: Multitasking in iOS - The Junction

| The Junction 30

APPLICATION LIFE CYCLETermination

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

Sunday, July 31, 2011

Page 49: Multitasking in iOS - The Junction

| The Junction 30

APPLICATION LIFE CYCLETermination

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

Sunday, July 31, 2011

Page 50: Multitasking in iOS - The Junction

| The Junction 30

APPLICATION LIFE CYCLETermination

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

applicationWillTerminate:

Sunday, July 31, 2011

Page 51: Multitasking in iOS - The Junction

| The Junction 30

APPLICATION LIFE CYCLETermination

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

applicationWillTerminate:

Sunday, July 31, 2011

Page 52: Multitasking in iOS - The Junction

| The Junction 30

APPLICATION LIFE CYCLETermination

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

applicationWillTerminate:

Sunday, July 31, 2011

Page 53: Multitasking in iOS - The Junction

| The Junction 31

LIFE CYCLE NOTIFICATIONS

UIApplicationDelegate Callback Notification

application:didFinishLaunchingWithOptions: UIApplicationDidFinishLaunchingNotification

applicationWillTerminate: UIApplicationWillTerminateNotification

applicationDidBecomeActive: UIApplicationDidBecomeActiveNotification

applicationWillResignActive: UIApplicationWillResignActiveNotification

applicationDidEnterBackground: UIApplicationDidEnterBackgroundNotification

applicationWillEnterForeground: UIApplicationWillEnterForegroundNotification

Sunday, July 31, 2011

Page 54: Multitasking in iOS - The Junction

| The Junction 32

Best Practices

Sunday, July 31, 2011

Page 55: Multitasking in iOS - The Junction

| The Junction 33

BEST PRACTICES OUTLINE

• System Resources

• Memory

• OpenGL

• Gracefully resuming from the background

• Preserving state

• Networking

• System notifications

Sunday, July 31, 2011

Page 56: Multitasking in iOS - The Junction

| The Junction 34

SYSTEM RESOURCES

17

Sunday, July 31, 2011

Page 57: Multitasking in iOS - The Junction

| The Junction 35

RESOURCE MANAGEMENT

• Apps share system resources:

• CPU

• I/O

• Memory

• GPU

• Network

Sunday, July 31, 2011

Page 58: Multitasking in iOS - The Junction

| The Junction 36

RESOURCE MANAGEMENT

• Goals

• Preserve foreground app usability

• Preserve battery life

• System resources are shared by all apps

• Multitasking apps should minimize CPU & memory

footprint in the background

Sunday, July 31, 2011

Page 59: Multitasking in iOS - The Junction

| The Junction 37

MEMORY

• Apps share a limited amount of memory

• iOS ensures it has the memory it needs

• Sending apps memory warnings

• Terminates apps

• Priority

• Suspended (high memory usage)

• Background (high memory usage)

• Suspended (low memory usage)

• Background (low memory usage)

• ...

• “Termination due to low memory is a normal condition”

Sunday, July 31, 2011

Page 60: Multitasking in iOS - The Junction

| The Junction 38

MEMORY

• iOS sends notifications to running apps to free memory

• Only sends warnings when freeing memory is crucial

• Suspended apps do not receive memory warnings!!!

• Reduce memory use in applicationDidEnterBackground:

Memory Warnings

Sunday, July 31, 2011

Page 61: Multitasking in iOS - The Junction

| The Junction 39

MEMORY

• OS and frameworks free some memory going to the background

• Apps using less than 16 MB of dirty memory are written to disk

• Balance memory footprint and speed to resume

Sunday, July 31, 2011

Page 62: Multitasking in iOS - The Junction

Backing Store

| The Junction 40

MEMORYView Backing Stores

CALayer

UIView

Sunday, July 31, 2011

Page 63: Multitasking in iOS - The Junction

| The Junction 41

MEMORYView Backing Stores

• Every UIView has a CALayer

• UIViews that draw themselves have bitmap backing stores

• iOS frees backing stores

• iOS may reclaim backing stores while app is running in the

background

• If reclaimed, iOS will call your -drawRect method

Backing Store

CALayer

UIView

Sunday, July 31, 2011

Page 64: Multitasking in iOS - The Junction

| The Junction 42

MEMORYUIImage Cache

• UIKit caches images loaded with -[UIImage imageName:]

• Cache is purged on entering the background

Sunday, July 31, 2011

Page 65: Multitasking in iOS - The Junction

| The Junction 43

MEMORYDisk Caches

• Many frameworks cache data in memory

• SQLite

• Core Data

• NSCache

• ...

• Caches are emptied automatically when entering background

Sunday, July 31, 2011

Page 66: Multitasking in iOS - The Junction

| The Junction 44

MEMORYUIImageViews

CALayer

UIView

• UIImageViews also have CALayer

• CALayer uses the image directly

• Images are not automatically reclaimed

• Detach large images from the view hierarchy

• Unload offscreen UIImageViews

• Beware of decompression on resume

• “The more you release the more time you need to resume”

Sunday, July 31, 2011

Page 67: Multitasking in iOS - The Junction

| The Junction 45

MEMORYCaches

• Flush application caches

• But not if resuming takes as long as resuming

• Consider using NSCache and NSPurgeableData:

• NSCache

• Objects are in memory

• Evicts objects as necessary

• Evicts objects when entering the background

• NSPurgeableData objects in an NSCache are not evicted

• Instead they become reclaimable when not in use

Sunday, July 31, 2011

Page 68: Multitasking in iOS - The Junction

| The Junction 46

MEMORYSummary

• Free memory when you receive a memory warning

• Remove UIImageViews from the view hierarchy unless

decompression is a problem

• Use NSCache and memory-mapped files when appropriate

• Balance memory footprint and speed to resume

Sunday, July 31, 2011

Page 69: Multitasking in iOS - The Junction

| The Junction 47

OPEN GL

• iOS terminates apps that use OpenGL in the background

• Stop animation timer when entering the background

Sunday, July 31, 2011

Page 70: Multitasking in iOS - The Junction

| The Junction 48

OPEN GL

Exception Type: EXC_CRASH (SIGABRT)

...

Thread 0 Crashed:0 libsystem_kernel.dylib 0x33b35a1c __pthread_kill

1 libsystem_c.dylib 0x30bac3b4 pthread_kill

2 libsystem_c.dylib 0x30ba4bf8 abort

3 IMGSGX535GLDriver 0x355229ae glrReturnNotPermittedKillClient

...

7 OpenGLES 0x354ade4e glFinish

...

40

Foreground

Active

Inactive

Not Running

Background

Running

Suspended

Try it yourself

Sunday, July 31, 2011

Page 71: Multitasking in iOS - The Junction

| The Junction 49

PRESERVING STATECommon State

• Return to exactly where you left off

• Save common UI states

• Selected tab bar

• Scroll position

Sunday, July 31, 2011

Page 72: Multitasking in iOS - The Junction

| The Junction 50

PRESERVING STATEApp-specific content

• User input, like the last number entered in Calculator

• For networking apps, save last search query

• ...

Sunday, July 31, 2011

Page 73: Multitasking in iOS - The Junction

| The Junction 51

PRESERVING STATEGames

• Turn-by-turn games should save after each turn

• Games with substantial, frequent state updates save periodically

• When the application enters the background

• Between levels, rooms

Sunday, July 31, 2011

Page 74: Multitasking in iOS - The Junction

| The Junction 52

NETWORKINGSockets and suspension

• Sockets may disconnect while suspended

• Be prepared for errors on resume

• Suspended apps cannot accept incoming connections

• Close listening sockets before suspend

• Reopen listening sockets on resume

Sunday, July 31, 2011

Page 75: Multitasking in iOS - The Junction

| The Junction 53

SYSTEM NOTIFICATIONS

• System change notifications not delivered to suspended app

• System queues notifications

• Delivered when app resumes

• Preferences and Locale may be changed in settings app

Event Notification

Preference changed in Settings NSUserDefaultsDidChangeNotification

Language or locale change NSCurrentLocaleDidChangeNotification

Sunday, July 31, 2011

Page 76: Multitasking in iOS - The Junction

| The Junction 54

Multitasking And Development

Sunday, July 31, 2011

Page 77: Multitasking in iOS - The Junction

| The Junction 55

INSTRUMENTS

• App life cycles events are flagged• App life cycle events are flagged

51

Sunday, July 31, 2011

Page 78: Multitasking in iOS - The Junction

| The Junction 56

SIMULATOR

• Simulator is not a replacement for device

• Supported

• Fast app switching

• Task completion

• Local notifications

• Multitasking UI (tray bar)

• Not supported

• Background audio, location and VoIP

• Significant location change, region monitoring

• Push notifications

Sunday, July 31, 2011

Page 79: Multitasking in iOS - The Junction

| The Junction 57

DEBUGGER

• Debugging app changes some behavior!!!

• Must-have to test outside the debugger

• No time limit in UIApplicationDelegate callbacks

• No time limit in task completion handler

Sunday, July 31, 2011

Page 80: Multitasking in iOS - The Junction

| The Junction 58

It takes leadership to innovate

Sunday, July 31, 2011