Multitasking in iOS - The Junction
Upcoming SlideShare
Loading in...5
×
 

Multitasking in iOS - The Junction

on

  • 6,414 views

iOS advanced engineering session focused on multitasking presented at The Junction

iOS advanced engineering session focused on multitasking presented at The Junction

Statistics

Views

Total Views
6,414
Views on SlideShare
5,469
Embed Views
945

Actions

Likes
2
Downloads
35
Comments
0

6 Embeds 945

http://www.tawkon.com 924
http://www.linkedin.com 14
https://www.linkedin.com 3
http://webcache.googleusercontent.com 2
http://translate.googleusercontent.com 1
http://paper.li 1

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Multitasking in iOS - The Junction Multitasking in iOS - The Junction Presentation Transcript

  • | The JunctionSunday, July 31, 2011
  • Multitasking in iOS The Junction Ori Goshen Co-Founder & Chief Architect mail: ori@tawkon.com twitter: @origoshen | The Junction 2Sunday, July 31, 2011
  • OUTLINE • Introduction • Multitasking In iOS Overview • Application Life Cycle • Best Practices • Multitasking & Development Tools | The Junction 3Sunday, July 31, 2011
  • Introduction | The Junction 4Sunday, July 31, 2011
  • 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
  • ABOUT TAWKON Drive Safely Eat Wisely Talk Responsibly | The Junction 6Sunday, July 31, 2011
  • ABOUT TAWKON | The Junction 7Sunday, July 31, 2011
  • HOW APPLE THINKS? | The Junction 8Sunday, July 31, 2011
  • 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
  • Multitasking In iOS Overview | The Junction 10Sunday, July 31, 2011
  • 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
  • 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
  • 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
  • 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
  • | The Junction 15Sunday, July 31, 2011
  • DO YOU REALLY NEED TO MULTITASK? | The Junction 16Sunday, July 31, 2011
  • 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
  • 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
  • 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
  • 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
  • LOCATION NAVIGATION • Keep users informed of their location in the background • Gives turn-by-turn directions | The Junction 21Sunday, July 31, 2011
  • 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
  • LOCATION TRACKING Significant location changes Significant location changes Location Location Location changed changed changed 57 | The Junction 23Sunday, July 31, 2011
  • LOCATION TRACKING Region monitoring Region monitoring Entered region 62 | The Junction 24Sunday, July 31, 2011
  • 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
  • Application Life Cycle | The Junction 26Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Launch and active/inactive Foreground Active Inactive Not Running Background Running Suspended | The Junction 27Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Launch and active/inactive Foreground Active Inactive Not Running Background Running Suspended | The Junction 27Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Launch and active/inactive Foreground Active Inactive applicationDidFinishLaunchingWith Options: Not Running Background Running Suspended | The Junction 27Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Launch and active/inactive Foreground Active Inactive applicationDidFinishLaunchingWith Options: Not Running Background Running Suspended | The Junction 27Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Launch and active/inactive Foreground applicationDidBecomeActive: Active Inactive applicationDidFinishLaunchingWith Options: Not Running Background Running Suspended | The Junction 27Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Launch and active/inactive Foreground applicationDidBecomeActive: Active Inactive applicationDidFinishLaunchingWith Options: Not Running Background Running Suspended | The Junction 27Sunday, July 31, 2011
  • 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
  • APPLICATION LIFE CYCLE Switch from an app Foreground Active Inactive Not Running Background Running Suspended | The Junction 28Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Switch from an app Foreground Active Inactive Not Running Background Running Suspended | The Junction 28Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Switch from an app Foreground Active applicationWillResignActive: Inactive Not Running Background Running Suspended | The Junction 28Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Switch from an app Foreground Active applicationWillResignActive: Inactive Not Running Background Running Suspended | The Junction 28Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Switch from an app Foreground Active applicationWillResignActive: Inactive applicationDidEnterBackground: Not Running Background Running Suspended | The Junction 28Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Switch from an app Foreground Active applicationWillResignActive: Inactive applicationDidEnterBackground: Not Running Background Running Suspended | The Junction 28Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Switch from an app Foreground Active applicationWillResignActive: Inactive applicationDidEnterBackground: Not Running Background Running Suspended | The Junction 28Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Switch to an app Foreground Active Inactive Not Running Background Running Suspended | The Junction 29Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Switch to an app Foreground Active Inactive Not Running Background Running Suspended | The Junction 29Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Switch to an app Foreground Active Inactive Not Running Background Running Suspended | The Junction 29Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Switch to an app Foreground Active Inactive Not Running Background Running Suspended | The Junction 29Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Switch to an app Foreground Active Inactive applicationDidEnterForeground: Not Running Background Running Suspended | The Junction 29Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Switch to an app Foreground Active Inactive applicationDidEnterForeground: Not Running Background Running Suspended | The Junction 29Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Switch to an app Foreground Active applicationDidBecomeActive: Inactive applicationDidEnterForeground: Not Running Background Running Suspended | The Junction 29Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Termination Foreground Active Inactive Not Running Background Running Suspended | The Junction 30Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Termination Foreground Active Inactive Not Running Background Running Suspended | The Junction 30Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Termination Foreground Active Inactive Not Running applicationWillTerminate: Background Running Suspended | The Junction 30Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Termination Foreground Active Inactive Not Running applicationWillTerminate: Background Running Suspended | The Junction 30Sunday, July 31, 2011
  • APPLICATION LIFE CYCLE Termination Foreground Active Inactive Not Running applicationWillTerminate: Background Running Suspended | The Junction 30Sunday, July 31, 2011
  • 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
  • Best Practices | The Junction 32Sunday, July 31, 2011
  • BEST PRACTICES OUTLINE • System Resources • Memory • OpenGL • Gracefully resuming from the background • Preserving state • Networking • System notifications | The Junction 33Sunday, July 31, 2011
  • SYSTEM RESOURCES 17 | The Junction 34Sunday, July 31, 2011
  • RESOURCE MANAGEMENT • Apps share system resources: • CPU • I/O • Memory • GPU • Network | The Junction 35Sunday, July 31, 2011
  • 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
  • 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
  • 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
  • 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
  • MEMORY View Backing Stores Backing Store CALayer UIView | The Junction 40Sunday, July 31, 2011
  • 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
  • MEMORY UIImage Cache • UIKit caches images loaded with -[UIImage imageName:] • Cache is purged on entering the background | The Junction 42Sunday, July 31, 2011
  • 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
  • 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
  • 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
  • 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
  • OPEN GL • iOS terminates apps that use OpenGL in the background • Stop animation timer when entering the background | The Junction 47Sunday, July 31, 2011
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Multitasking And Development | The Junction 54Sunday, July 31, 2011
  • INSTRUMENTS • •Applife cycle eventsevents are flagged App life cycles are flagged 51 | The Junction 55Sunday, July 31, 2011
  • 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
  • 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
  • It takes leadership to innovate | The Junction 58Sunday, July 31, 2011