2. Multitasking in iOS
The Junction
Ori Goshen
Co-Founder & Chief Architect
mail: ori@tawkon.com
twitter: @origoshen
| The Junction 2
Sunday, July 31, 2011
3. OUTLINE
• Introduction
• Multitasking In iOS Overview
• Application Life Cycle
• Best Practices
• Multitasking & Development Tools
| The Junction 3
Sunday, July 31, 2011
4. Introduction
| The Junction 4
Sunday, July 31, 2011
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
| The Junction 5
Sunday, July 31, 2011
6. ABOUT TAWKON
Drive Safely Eat Wisely Talk Responsibly
| The Junction 6
Sunday, July 31, 2011
7. ABOUT TAWKON
| The Junction 7
Sunday, July 31, 2011
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)
| The Junction 9
Sunday, July 31, 2011
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
| The Junction 11
Sunday, July 31, 2011
12. iOS MULTITASKING PHILOSOPHY
• “General purpose concurrency is not the solution for
mobile devices”
Background execution:
finish task, audio,
location, VoIP
Push
Notification
Fast App
Switching
| The Junction 12
Sunday, July 31, 2011
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)
| The Junction 13
Sunday, July 31, 2011
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
| The Junction 14
Sunday, July 31, 2011
16. DO YOU REALLY NEED TO MULTITASK?
| The Junction 16
Sunday, July 31, 2011
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!)
| The Junction 17
Sunday, July 31, 2011
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”
| The Junction 18
Sunday, July 31, 2011
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
| The Junction 19
Sunday, July 31, 2011
20. BACKGROUND AUDIO
• Plays audible content to the user
• Streams audio
• Record audio
• Continue playing/recording in the background
• Integrates with remote controls
| The Junction 20
Sunday, July 31, 2011
21. LOCATION NAVIGATION
• Keep users informed of their location in the
background
• Gives turn-by-turn directions
| The Junction 21
Sunday, July 31, 2011
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, ..)
| The Junction 22
Sunday, July 31, 2011
24. LOCATION TRACKING
Region monitoring
Region monitoring
Entered
region
62
| The Junction 24
Sunday, July 31, 2011
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
| The Junction 25
Sunday, July 31, 2011
27. APPLICATION LIFE CYCLE
Launch and active/inactive
Foreground
Active
Inactive
Not Running
Background
Running
Suspended
| The Junction 27
Sunday, July 31, 2011
28. APPLICATION LIFE CYCLE
Launch and active/inactive
Foreground
Active
Inactive
Not Running
Background
Running
Suspended
| The Junction 27
Sunday, July 31, 2011
29. APPLICATION LIFE CYCLE
Launch and active/inactive
Foreground
Active
Inactive
applicationDidFinishLaunchingWith
Options: Not Running
Background
Running
Suspended
| The Junction 27
Sunday, July 31, 2011
30. APPLICATION LIFE CYCLE
Launch and active/inactive
Foreground
Active
Inactive
applicationDidFinishLaunchingWith
Options: Not Running
Background
Running
Suspended
| The Junction 27
Sunday, July 31, 2011
31. APPLICATION LIFE CYCLE
Launch and active/inactive
Foreground
applicationDidBecomeActive: Active
Inactive
applicationDidFinishLaunchingWith
Options: Not Running
Background
Running
Suspended
| The Junction 27
Sunday, July 31, 2011
32. APPLICATION LIFE CYCLE
Launch and active/inactive
Foreground
applicationDidBecomeActive: Active
Inactive
applicationDidFinishLaunchingWith
Options: Not Running
Background
Running
Suspended
| The Junction 27
Sunday, July 31, 2011
33. APPLICATION LIFE CYCLE
Launch and active/inactive
Foreground
applicationDidBecomeActive: Active
applicationWillResignActive:
Inactive
applicationDidFinishLaunchingWith
Options: Not Running
Background
Running
Suspended
| The Junction 27
Sunday, July 31, 2011
34. APPLICATION LIFE CYCLE
Switch from an app
Foreground
Active
Inactive
Not Running
Background
Running
Suspended
| The Junction 28
Sunday, July 31, 2011
35. APPLICATION LIFE CYCLE
Switch from an app
Foreground
Active
Inactive
Not Running
Background
Running
Suspended
| The Junction 28
Sunday, July 31, 2011
36. APPLICATION LIFE CYCLE
Switch from an app
Foreground
Active
applicationWillResignActive:
Inactive
Not Running
Background
Running
Suspended
| The Junction 28
Sunday, July 31, 2011
37. APPLICATION LIFE CYCLE
Switch from an app
Foreground
Active
applicationWillResignActive:
Inactive
Not Running
Background
Running
Suspended
| The Junction 28
Sunday, July 31, 2011
38. APPLICATION LIFE CYCLE
Switch from an app
Foreground
Active
applicationWillResignActive:
Inactive
applicationDidEnterBackground:
Not Running
Background
Running
Suspended
| The Junction 28
Sunday, July 31, 2011
39. APPLICATION LIFE CYCLE
Switch from an app
Foreground
Active
applicationWillResignActive:
Inactive
applicationDidEnterBackground:
Not Running
Background
Running
Suspended
| The Junction 28
Sunday, July 31, 2011
40. APPLICATION LIFE CYCLE
Switch from an app
Foreground
Active
applicationWillResignActive:
Inactive
applicationDidEnterBackground:
Not Running
Background
Running
Suspended
| The Junction 28
Sunday, July 31, 2011
41. APPLICATION LIFE CYCLE
Switch to an app
Foreground
Active
Inactive
Not Running
Background
Running
Suspended
| The Junction 29
Sunday, July 31, 2011
42. APPLICATION LIFE CYCLE
Switch to an app
Foreground
Active
Inactive
Not Running
Background
Running
Suspended
| The Junction 29
Sunday, July 31, 2011
43. APPLICATION LIFE CYCLE
Switch to an app
Foreground
Active
Inactive
Not Running
Background
Running
Suspended
| The Junction 29
Sunday, July 31, 2011
44. APPLICATION LIFE CYCLE
Switch to an app
Foreground
Active
Inactive
Not Running
Background
Running
Suspended
| The Junction 29
Sunday, July 31, 2011
45. APPLICATION LIFE CYCLE
Switch to an app
Foreground
Active
Inactive
applicationDidEnterForeground:
Not Running
Background
Running
Suspended
| The Junction 29
Sunday, July 31, 2011
46. APPLICATION LIFE CYCLE
Switch to an app
Foreground
Active
Inactive
applicationDidEnterForeground:
Not Running
Background
Running
Suspended
| The Junction 29
Sunday, July 31, 2011
47. APPLICATION LIFE CYCLE
Switch to an app
Foreground
Active
applicationDidBecomeActive:
Inactive
applicationDidEnterForeground:
Not Running
Background
Running
Suspended
| The Junction 29
Sunday, July 31, 2011
48. APPLICATION LIFE CYCLE
Termination
Foreground
Active
Inactive
Not Running
Background
Running
Suspended
| The Junction 30
Sunday, July 31, 2011
49. APPLICATION LIFE CYCLE
Termination
Foreground
Active
Inactive
Not Running
Background
Running
Suspended
| The Junction 30
Sunday, July 31, 2011
50. APPLICATION LIFE CYCLE
Termination
Foreground
Active
Inactive
Not Running
applicationWillTerminate:
Background
Running
Suspended
| The Junction 30
Sunday, July 31, 2011
51. APPLICATION LIFE CYCLE
Termination
Foreground
Active
Inactive
Not Running
applicationWillTerminate:
Background
Running
Suspended
| The Junction 30
Sunday, July 31, 2011
52. APPLICATION LIFE CYCLE
Termination
Foreground
Active
Inactive
Not Running
applicationWillTerminate:
Background
Running
Suspended
| The Junction 30
Sunday, July 31, 2011
53. LIFE CYCLE NOTIFICATIONS
UIApplicationDelegate Callback Notification
application:didFinishLaunchingWithOptions: UIApplicationDidFinishLaunchingNotification
applicationWillTerminate: UIApplicationWillTerminateNotification
applicationDidBecomeActive: UIApplicationDidBecomeActiveNotification
applicationWillResignActive: UIApplicationWillResignActiveNotification
applicationDidEnterBackground: UIApplicationDidEnterBackgroundNotification
applicationWillEnterForeground: UIApplicationWillEnterForegroundNotification
| The Junction 31
Sunday, July 31, 2011
55. BEST PRACTICES OUTLINE
• System Resources
• Memory
• OpenGL
• Gracefully resuming from the background
• Preserving state
• Networking
• System notifications
| The Junction 33
Sunday, July 31, 2011
57. RESOURCE MANAGEMENT
• Apps share system resources:
• CPU
• I/O
• Memory
• GPU
• Network
| The Junction 35
Sunday, July 31, 2011
58. 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
| The Junction 36
Sunday, July 31, 2011
59. 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”
| The Junction 37
Sunday, July 31, 2011
60. MEMORY
Memory Warnings
• 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:
| The Junction 38
Sunday, July 31, 2011
61. 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
| The Junction 39
Sunday, July 31, 2011
62. MEMORY
View Backing Stores
Backing Store
CALayer
UIView
| The Junction 40
Sunday, July 31, 2011
63. MEMORY
View 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
| The Junction 41
Sunday, July 31, 2011
64. MEMORY
UIImage Cache
• UIKit caches images loaded with -[UIImage imageName:]
• Cache is purged on entering the background
| The Junction 42
Sunday, July 31, 2011
65. MEMORY
Disk Caches
• Many frameworks cache data in memory
• SQLite
• Core Data
• NSCache
• ...
• Caches are emptied automatically when entering background
| The Junction 43
Sunday, July 31, 2011
66. CALayer
MEMORY
UIView
UIImageViews
• 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”
| The Junction 44
Sunday, July 31, 2011
67. MEMORY
Caches
• 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
| The Junction 45
Sunday, July 31, 2011
68. MEMORY
Summary
• 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
| The Junction 46
Sunday, July 31, 2011
69. OPEN GL
• iOS terminates apps that use OpenGL in the background
• Stop animation timer when entering the background
| The Junction 47
Sunday, July 31, 2011
70. OPEN GL
Try it yourself
Exception Type: EXC_CRASH (SIGABRT)
... Foreground
Thread 0 Crashed:
Active
0 libsystem_kernel.dylib 0x33b35a1c __pthread_kill
1 libsystem_c.dylib 0x30bac3b4 pthread_kill
2 libsystem_c.dylib 0x30ba4bf8 abort
Inactive
3 IMGSGX535GLDriver 0x355229ae glrReturnNotPermittedKillClient
...
7 OpenGLES 0x354ade4e glFinish
... Not Running
Background
40 Running
Suspended
| The Junction 48
Sunday, July 31, 2011
71. PRESERVING STATE
Common State
• Return to exactly where you left off
• Save common UI states
• Selected tab bar
• Scroll position
| The Junction 49
Sunday, July 31, 2011
72. PRESERVING STATE
App-specific content
• User input, like the last number entered in Calculator
• For networking apps, save last search query
• ...
| The Junction 50
Sunday, July 31, 2011
73. PRESERVING STATE
Games
• 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
| The Junction 51
Sunday, July 31, 2011
74. NETWORKING
Sockets 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
| The Junction 52
Sunday, July 31, 2011
75. 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
| The Junction 53
Sunday, July 31, 2011
77. INSTRUMENTS
• •Applife cycle eventsevents are flagged
App
life cycles
are flagged
51
| The Junction 55
Sunday, July 31, 2011
78. 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
| The Junction 56
Sunday, July 31, 2011
79. 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
| The Junction 57
Sunday, July 31, 2011