Multitasking in iOS - The Junction

6,424
-1

Published on

iOS advanced engineering session focused on multitasking presented at The Junction

Published in: Technology, News & Politics
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
6,424
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
42
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Multitasking in iOS - The Junction

  1. 1. | The JunctionSunday, July 31, 2011
  2. 2. Multitasking in iOS The Junction Ori Goshen Co-Founder & Chief Architect mail: ori@tawkon.com twitter: @origoshen | The Junction 2Sunday, July 31, 2011
  3. 3. OUTLINE • Introduction • Multitasking In iOS Overview • Application Life Cycle • Best Practices • Multitasking & Development Tools | The Junction 3Sunday, July 31, 2011
  4. 4. Introduction | The Junction 4Sunday, July 31, 2011
  5. 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 5Sunday, July 31, 2011
  6. 6. ABOUT TAWKON Drive Safely Eat Wisely Talk Responsibly | The Junction 6Sunday, July 31, 2011
  7. 7. ABOUT TAWKON | The Junction 7Sunday, July 31, 2011
  8. 8. HOW APPLE THINKS? | The Junction 8Sunday, July 31, 2011
  9. 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 9Sunday, July 31, 2011
  10. 10. Multitasking In iOS Overview | The Junction 10Sunday, July 31, 2011
  11. 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 11Sunday, July 31, 2011
  12. 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 12Sunday, July 31, 2011
  13. 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 13Sunday, July 31, 2011
  14. 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 14Sunday, July 31, 2011
  15. 15. | The Junction 15Sunday, July 31, 2011
  16. 16. DO YOU REALLY NEED TO MULTITASK? | The Junction 16Sunday, July 31, 2011
  17. 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 17Sunday, July 31, 2011
  18. 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 18Sunday, July 31, 2011
  19. 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 19Sunday, July 31, 2011
  20. 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 20Sunday, July 31, 2011
  21. 21. LOCATION NAVIGATION • Keep users informed of their location in the background • Gives turn-by-turn directions | The Junction 21Sunday, July 31, 2011
  22. 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 22Sunday, July 31, 2011
  23. 23. LOCATION TRACKING Significant location changes Significant location changes Location Location Location changed changed changed 57 | The Junction 23Sunday, July 31, 2011
  24. 24. LOCATION TRACKING Region monitoring Region monitoring Entered region 62 | The Junction 24Sunday, July 31, 2011
  25. 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 25Sunday, July 31, 2011
  26. 26. Application Life Cycle | The Junction 26Sunday, July 31, 2011
  27. 27. APPLICATION LIFE CYCLE Launch and active/inactive Foreground Active Inactive Not Running Background Running Suspended | The Junction 27Sunday, July 31, 2011
  28. 28. APPLICATION LIFE CYCLE Launch and active/inactive Foreground Active Inactive Not Running Background Running Suspended | The Junction 27Sunday, July 31, 2011
  29. 29. APPLICATION LIFE CYCLE Launch and active/inactive Foreground Active Inactive applicationDidFinishLaunchingWith Options: Not Running Background Running Suspended | The Junction 27Sunday, July 31, 2011
  30. 30. APPLICATION LIFE CYCLE Launch and active/inactive Foreground Active Inactive applicationDidFinishLaunchingWith Options: Not Running Background Running Suspended | The Junction 27Sunday, July 31, 2011
  31. 31. APPLICATION LIFE CYCLE Launch and active/inactive Foreground applicationDidBecomeActive: Active Inactive applicationDidFinishLaunchingWith Options: Not Running Background Running Suspended | The Junction 27Sunday, July 31, 2011
  32. 32. APPLICATION LIFE CYCLE Launch and active/inactive Foreground applicationDidBecomeActive: Active Inactive applicationDidFinishLaunchingWith Options: Not Running Background Running Suspended | The Junction 27Sunday, July 31, 2011
  33. 33. APPLICATION LIFE CYCLE Launch and active/inactive Foreground applicationDidBecomeActive: Active applicationWillResignActive: Inactive applicationDidFinishLaunchingWith Options: Not Running Background Running Suspended | The Junction 27Sunday, July 31, 2011
  34. 34. APPLICATION LIFE CYCLE Switch from an app Foreground Active Inactive Not Running Background Running Suspended | The Junction 28Sunday, July 31, 2011
  35. 35. APPLICATION LIFE CYCLE Switch from an app Foreground Active Inactive Not Running Background Running Suspended | The Junction 28Sunday, July 31, 2011
  36. 36. APPLICATION LIFE CYCLE Switch from an app Foreground Active applicationWillResignActive: Inactive Not Running Background Running Suspended | The Junction 28Sunday, July 31, 2011
  37. 37. APPLICATION LIFE CYCLE Switch from an app Foreground Active applicationWillResignActive: Inactive Not Running Background Running Suspended | The Junction 28Sunday, July 31, 2011
  38. 38. APPLICATION LIFE CYCLE Switch from an app Foreground Active applicationWillResignActive: Inactive applicationDidEnterBackground: Not Running Background Running Suspended | The Junction 28Sunday, July 31, 2011
  39. 39. APPLICATION LIFE CYCLE Switch from an app Foreground Active applicationWillResignActive: Inactive applicationDidEnterBackground: Not Running Background Running Suspended | The Junction 28Sunday, July 31, 2011
  40. 40. APPLICATION LIFE CYCLE Switch from an app Foreground Active applicationWillResignActive: Inactive applicationDidEnterBackground: Not Running Background Running Suspended | The Junction 28Sunday, July 31, 2011
  41. 41. APPLICATION LIFE CYCLE Switch to an app Foreground Active Inactive Not Running Background Running Suspended | The Junction 29Sunday, July 31, 2011
  42. 42. APPLICATION LIFE CYCLE Switch to an app Foreground Active Inactive Not Running Background Running Suspended | The Junction 29Sunday, July 31, 2011
  43. 43. APPLICATION LIFE CYCLE Switch to an app Foreground Active Inactive Not Running Background Running Suspended | The Junction 29Sunday, July 31, 2011
  44. 44. APPLICATION LIFE CYCLE Switch to an app Foreground Active Inactive Not Running Background Running Suspended | The Junction 29Sunday, July 31, 2011
  45. 45. APPLICATION LIFE CYCLE Switch to an app Foreground Active Inactive applicationDidEnterForeground: Not Running Background Running Suspended | The Junction 29Sunday, July 31, 2011
  46. 46. APPLICATION LIFE CYCLE Switch to an app Foreground Active Inactive applicationDidEnterForeground: Not Running Background Running Suspended | The Junction 29Sunday, July 31, 2011
  47. 47. APPLICATION LIFE CYCLE Switch to an app Foreground Active applicationDidBecomeActive: Inactive applicationDidEnterForeground: Not Running Background Running Suspended | The Junction 29Sunday, July 31, 2011
  48. 48. APPLICATION LIFE CYCLE Termination Foreground Active Inactive Not Running Background Running Suspended | The Junction 30Sunday, July 31, 2011
  49. 49. APPLICATION LIFE CYCLE Termination Foreground Active Inactive Not Running Background Running Suspended | The Junction 30Sunday, July 31, 2011
  50. 50. APPLICATION LIFE CYCLE Termination Foreground Active Inactive Not Running applicationWillTerminate: Background Running Suspended | The Junction 30Sunday, July 31, 2011
  51. 51. APPLICATION LIFE CYCLE Termination Foreground Active Inactive Not Running applicationWillTerminate: Background Running Suspended | The Junction 30Sunday, July 31, 2011
  52. 52. APPLICATION LIFE CYCLE Termination Foreground Active Inactive Not Running applicationWillTerminate: Background Running Suspended | The Junction 30Sunday, July 31, 2011
  53. 53. LIFE CYCLE NOTIFICATIONS UIApplicationDelegate Callback Notification application:didFinishLaunchingWithOptions: UIApplicationDidFinishLaunchingNotification applicationWillTerminate: UIApplicationWillTerminateNotification applicationDidBecomeActive: UIApplicationDidBecomeActiveNotification applicationWillResignActive: UIApplicationWillResignActiveNotification applicationDidEnterBackground: UIApplicationDidEnterBackgroundNotification applicationWillEnterForeground: UIApplicationWillEnterForegroundNotification | The Junction 31Sunday, July 31, 2011
  54. 54. Best Practices | The Junction 32Sunday, July 31, 2011
  55. 55. BEST PRACTICES OUTLINE • System Resources • Memory • OpenGL • Gracefully resuming from the background • Preserving state • Networking • System notifications | The Junction 33Sunday, July 31, 2011
  56. 56. SYSTEM RESOURCES 17 | The Junction 34Sunday, July 31, 2011
  57. 57. RESOURCE MANAGEMENT • Apps share system resources: • CPU • I/O • Memory • GPU • Network | The Junction 35Sunday, July 31, 2011
  58. 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 36Sunday, July 31, 2011
  59. 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 37Sunday, July 31, 2011
  60. 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 38Sunday, July 31, 2011
  61. 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 39Sunday, July 31, 2011
  62. 62. MEMORY View Backing Stores Backing Store CALayer UIView | The Junction 40Sunday, July 31, 2011
  63. 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 41Sunday, July 31, 2011
  64. 64. MEMORY UIImage Cache • UIKit caches images loaded with -[UIImage imageName:] • Cache is purged on entering the background | The Junction 42Sunday, July 31, 2011
  65. 65. MEMORY Disk Caches • Many frameworks cache data in memory • SQLite • Core Data • NSCache • ... • Caches are emptied automatically when entering background | The Junction 43Sunday, July 31, 2011
  66. 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 44Sunday, July 31, 2011
  67. 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 45Sunday, July 31, 2011
  68. 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 46Sunday, July 31, 2011
  69. 69. OPEN GL • iOS terminates apps that use OpenGL in the background • Stop animation timer when entering the background | The Junction 47Sunday, July 31, 2011
  70. 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 48Sunday, July 31, 2011
  71. 71. PRESERVING STATE Common State • Return to exactly where you left off • Save common UI states • Selected tab bar • Scroll position | The Junction 49Sunday, July 31, 2011
  72. 72. PRESERVING STATE App-specific content • User input, like the last number entered in Calculator • For networking apps, save last search query • ... | The Junction 50Sunday, July 31, 2011
  73. 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 51Sunday, July 31, 2011
  74. 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 52Sunday, July 31, 2011
  75. 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 53Sunday, July 31, 2011
  76. 76. Multitasking And Development | The Junction 54Sunday, July 31, 2011
  77. 77. INSTRUMENTS • •Applife cycle eventsevents are flagged App life cycles are flagged 51 | The Junction 55Sunday, July 31, 2011
  78. 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 56Sunday, July 31, 2011
  79. 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 57Sunday, July 31, 2011
  80. 80. It takes leadership to innovate | The Junction 58Sunday, July 31, 2011
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×