
Adopting Multitasking on iPhone OS Part 1 David Myszewski, David Goodwin iPhone Performance 2 Introduction • Multitasking is a major feature of iOS 4 • Improves user experience • Enhances existing apps • Enables new app classes • Every app should adopt multitasking 3 What You’ll Learn • Overview of iOS 4 multitasking • How to enable multitasking in your app ■ APIs ■ Responsibilities ■ Best practices • Using development tools to implement multitasking 4 Demo iPhone OS 3 vs. iOS 4 5 iOS 4 Multitasking Philosophy • General purpose concurrency is not the solution for mobile devices FinishBenefit task, from audio, location,background VoIP execution Push notifications Switch quickly, preserve state Don’t benefit from background execution 6 Fast App Switching Multitasking benefits for all iOS 4 apps • App resumes immediately • App state is preserved • Tight integration with multitasking UI • Demonstrates supported, up-to-date app 7 Understanding 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 8 Understanding 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 9 Multitasking Services Details Mission Adopting Multitasking on iPhone OS, Part 2 Tuesday, 3:15PM-4:15PM 10 How to Enable Multitasking • Build with iPhone SDK 4 • Fast app switching enabled by default • Background audio, location, and VoIP require explicit declaration in app’s Info.plist 11 App Life Cycle 12 App Life Cycle iPhone OS 3 review Active Inactive Not Running 13 App Life Cycle iOS 4 Foreground Active Inactive Not Running Running Suspended Background 14 UIApplicationDelegate Callbacks Launch and active/inactive Foreground Active applicationDidBecomeActive: applicationWillResignActive: Inactive application: didFinishLaunchingWithOptions: Not Running Running Suspended 15 Background UIApplicationDelegate Callbacks Switching from an app Foreground Active applicationWillResignActive: Inactive applicationDidEnterBackground: Not Running Running Suspended X Background 16 UIApplicationDelegate Callbacks Switching to an app Foreground Active applicationDidBecomeActive: Inactive applicationWillEnterForeground: Not Running Running Suspended X Background 17 UIApplicationDelegateForeground Callbacks Active Termination Inactive Not Running applicationWillTerminate: Running X Suspended Background 18 Life Cycle Notifications UIApplicationDelegate Callback Notification application:didFinishLaunchingWithOptions: UIApplicationDidFinishLaunchingNotification applicationWillTerminate: UIApplicationWillTerminateNotification applicationDidBecomeActive: UIApplicationDidBecomeActiveNotification applicationWillResignActive: UIApplicationWillResignActiveNotification applicationDidEnterBackground: UIApplicationDidEnterBackgroundNotification applicationWillEnterForeground: UIApplicationWillEnterForegroundNotification 19 Responsibilities and Best Practices 20 System Resource Management • Goals ■ Preserve foreground app usability ■ Preserve battery life • System resources shared by all apps • Multitasking apps should minimize system footprint in the background 21 Responsibilities and Best Practices Outline • Entering the background ■ Save app state ■ Reduce memory usage ■ Prepare UI ■ Stop Bonjour and network listening ■ Stop GPU usage ■ Stop shared system data access • Resuming from suspend ■ Restore app state ■ Handle system changes ■ Restore networking 22 Saving App State Foreground Active • Save app state when entering the background Inactive • Save state incrementally ■ Only have a few seconds in Not Running applicationDidEnterBackground: Running applicationDidEnterBackground: Suspended Background 23 Responsibilities and Best Practices Outline • Entering the background ■ Save app state ■ Reduce memory usage ■ Prepare UI ■ Stop Bonjour and network listening ■ Stop GPU usage ■ Stop shared system data access • Resuming from suspend ■ Restore app state ■ Handle system changes ■ Restore networking 24 Foreground System Memory Active Inactive • Memory is a limited resource • System terminates apps when Not Running memory exhausted • Priority Running ■ Suspended (high memory usage) ■ Background running (high memory usage) Suspended ■ Suspended (low memory usage) Background ■ Background running (low memory usage) • Termination due to low memory is a normal condition 25 Reducing Memory Usage • Using less memory allows more apps to remain alive • Low memory usage increases chance that your app stays alive • Reduce memory use in applicationDidEnterBackground: 26 App Memory Usage • App state Journal • App files and databases Image … SQLite, CoreData, Entries NSCache, “flat” file, … • Views and layers • Backing stores • View controllers UIViewController Backing Graphic Store UIView CALayer 27 Reducing Memory Usage Reductions performed by system • Free backing graphic store Journal Release non-visible • Image … SQLite, CoreData, UIViewController views Entries NSCache, “flat” file, … • Flush [UIImage ImageNamed] cache UIViewController • Reclaim SQLite page cache, CoreData, NSCache Backing Graphic Store UIView CALayer 28 Reducing Memory Usage Reductions performed by your app • Release images Journal [myImage release]; Image … SQLite, CoreData, Entries NSCache, “flat” file, … • Flush caches of data that can be regenerated • For read-only or sparsely UIViewController written files use mmap() ■ System only uses memory Backing Graphic Store as needed UIView CALayer ■ Read-only memory can be reclaimed without terminating app 29 Reducing Memory Usage Tradeoffs • Typically don’t release view controllers Journal Image … SQLite, CoreData, • Other state—Must tradeoff Entries NSCache, “flat” file, … memory savings vs. time to recreate on resume UIViewController Backing Graphic Store UIView CALayer 30 Responsibilities and Best Practices Outline • Entering the background ■ Save app state ■ Reduce memory usage ■ Prepare UI ■ Stop Bonjour and network listening ■ Stop GPU usage ■ Stop shared system data access • Resuming from suspend ■ Restore app state ■ Handle system changes ■ Restore networking 31 Prepare UI for Background • Pause app if appropriate ■ Update UI to show pause ■ Typically performed in applicationDidResignActive: • Remove alerts and actionsheets if appropriate - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated • Prepare for screenshot ■ Hide sensitive information ■ Stop animations 32 Responsibilities and Best Practices Outline • Entering the background ■ Save app state ■ Reduce memory usage ■ Prepare UI ■ Stop Bonjour and network listening ■ Stop GPU usage ■ Stop shared system data access • Resuming from suspend ■ Restore app state ■ Handle system changes ■ Restore networking 33 Network Foreground Listening Sockets Active • Suspended app cannot respond to connection attempts Inactive • Close listening sockets before suspend Not Running • Reopen listening sockets on resume Running applicationDidEnterBackground: applicationWillEnterForeground: Suspended Background 34 Bonjour • Suspended app cannot accept incoming connection attempts ■ Should not advertise service ■ Should not browse for service • Bonjour operations may be cancelled while app suspended • When app resumes ■ Be prepared for errors ■ Restart Bonjour services if necessary ■ Update UI 35 GPU Foreground Active • GPU off limits in background— creating EAGLContext or issuing Inactive OpenGL commands results in termination Not Running • Enforced in background running state • Stop GPU usage before returning from Running applicationDidEnterBackground: applicationDidEnterBackground: Suspended Background 36 Shared System Data Foreground Active • Holding exclusive access to shared data while suspended not allowed— results in termination Inactive • Shared data accessed by API ■ Calendar Not Running ■ Address book ■ Music and Media libraries Running • Enforced upon entry to suspended state Suspended • Stop API usage before returning from Background applicationDidEnterBackground: 37 Responsibilities and Best Practices Outline • Entering the background ■ Save app state ■ Reduce memory usage ■ Prepare UI ■ Stop Bonjour and network listening ■ Stop GPU usage ■ Stop shared system data access • Resuming from suspend ■ Restore app state ■ Handle system changes ■ Restore networking 38 Restore App State Restores performed by system • Backing graphic store Journal ImageNamed cache will • Image … SQLite, CoreData, repopulate lazily as images Entries NSCache, “flat” file, … are requested • SQLite, CoreData, NSCache UIViewController restored as needed • Memory mapped files will Backing Graphic Store reload as needed UIView CALayer 39 Restore App State Best practices • No one right way to restore app state on resume • Tradeoff—Memory size reduction vs. time and effort to restore state • Consider lazy restore for your app data ■ Release objects as entering background ■ Restore objects on demand—As they are needed 40 Restore App State Lazy state restore - (MyData *) dataAtIndex:(unsigned)index { !if (_data[index] == nil) { !!_data[index] = [DataController loadDataAtIndex:index]; !![_data[index] retain]; !} !return _data[index]; } - (void)
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages58 Page
-
File Size-