19
(a dump of my own development notes while writing PlaceTrack ) iPHONE iOS4 / SDK4.0 multitasking support from a developer’s perspective Nico Tranquilli [email protected] www.tranquilli.org Sunday, February 26, 2012

iPHONE iOS4 / SDK4.0 multitasking supportlabs.tranquilli.org/docs/iOS4_background_tranquilli.pdf · Nico Tranquilli • July 2010 “iOS4 multitasking development notes” iPHONE

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: iPHONE iOS4 / SDK4.0 multitasking supportlabs.tranquilli.org/docs/iOS4_background_tranquilli.pdf · Nico Tranquilli • July 2010 “iOS4 multitasking development notes” iPHONE

(a dump of my own development notes while writing PlaceTrack)

iPHONE iOS4 / SDK4.0 multitasking support

from a developer’s perspective

Nico [email protected]

www.tranquilli.org

Sunday, February 26, 2012

Page 2: iPHONE iOS4 / SDK4.0 multitasking supportlabs.tranquilli.org/docs/iOS4_background_tranquilli.pdf · Nico Tranquilli • July 2010 “iOS4 multitasking development notes” iPHONE

www.tranquilli.orgNico Tranquilli • July 2010 “iOS4 multitasking development notes”

iPHONE MULTITASKING

quitting (pressing Home) a running application on iOS 4.0 causes it to become inactive and be moved to the background. Quitting from the task bar is equivalent to a force quit/termination (kill -9)

an application interrupted by a system event (incoming call, SMS, calendar notification) resigns the active state (ie. looses focus). Once the user accepts the interruption, it moves to the background and suspends

app.delegate’s methods get called on transitions between foreground and background states (and should be handled appropriately by the developer); other objects can observe appropriate notifications

older applications, linked against SDK earlier than 4.0 or running on hw with no multitasking support, always SUSPEND, are KEPT IN RAM (that’s for Fast App Switching) but are NOT going to be USING CPU CYCLES

Sunday, February 26, 2012

Page 3: iPHONE iOS4 / SDK4.0 multitasking supportlabs.tranquilli.org/docs/iOS4_background_tranquilli.pdf · Nico Tranquilli • July 2010 “iOS4 multitasking development notes” iPHONE

www.tranquilli.orgNico Tranquilli • July 2010 “iOS4 multitasking development notes”

MULTITASKING SUPPORT

iOS4 on 3G and earlier iPhone models doesn’t support multitasking

iOS earlier than 4.0 doesn’t support multitasking

any app built against a pre-4.0 SDK still behaves as it did on earlier OS (terminates when you leave and gets the same notifications)

when you leave an app built against a 4.0 SDK, it receives applicationDidEnterBackground. On (older) devices with no multitasking support, applicationWillTerminate is called afterwards.

developers can explicitly opt out by setting UIApplicationExitsOnSuspend to YES in Info.plist

Sunday, February 26, 2012

Page 4: iPHONE iOS4 / SDK4.0 multitasking supportlabs.tranquilli.org/docs/iOS4_background_tranquilli.pdf · Nico Tranquilli • July 2010 “iOS4 multitasking development notes” iPHONE

www.tranquilli.orgNico Tranquilli • July 2010 “iOS4 multitasking development notes”

APPLICATION’S STATES

ACTIVE‣ executing code in the foreground‣ receiving events‣ application has focus

INACTIVE

still executing code in the foreground but not receiving eventsapplications stay here:‣ on their way to the background state‣ when the system is waiting for the user to respond to events‣ when the user presses the Sleep/Wake button

BACKGROUNDexecuting code in the backgroundapplications stay here:‣ on their way to the suspended state (briefly)‣ if they asked for a (limited) extra processing time

SUSPENDED

‣ frozen in the background: not executing code but still in memory‣ push notifications still delivered when the user taps alert’s btn‣ events coalesced and delivered later when it resumes‣ wakes up and moves to the background on registered background events (eg. Significant Location Change)

Sunday, February 26, 2012

Page 5: iPHONE iOS4 / SDK4.0 multitasking supportlabs.tranquilli.org/docs/iOS4_background_tranquilli.pdf · Nico Tranquilli • July 2010 “iOS4 multitasking development notes” iPHONE

www.tranquilli.orgNico Tranquilli • July 2010 “iOS4 multitasking development notes”

BACKGROUND EXECUTION

once in the background, an application can...

suspend, shortly after entering this state and be woken up later by events delivered by system background services (Significant Location Change, VoIP) or when user taps a notification’s view button

execute your code for a short amount of time, continuing what it was doing before becoming INACTIVE and suspending once the task is complete (or the 600s time limit is reached)

keep running in the background for unlimited time, if it was registered for one of the permitted background task (eg. continuos location or background audio)

Sunday, February 26, 2012

Page 6: iPHONE iOS4 / SDK4.0 multitasking supportlabs.tranquilli.org/docs/iOS4_background_tranquilli.pdf · Nico Tranquilli • July 2010 “iOS4 multitasking development notes” iPHONE

www.tranquilli.orgNico Tranquilli • July 2010 “iOS4 multitasking development notes”

ENTERING BACKGROUND...

applicationWillResignActive is called on app.delegate (and UIApplicationWillResignActiveNotification is posted to registered objects)

➡ delivery of touch events is suspended by the system (inactive applications run but do not dispatch incoming events)

➡ you should pause ongoing tasks and wait to transition to either the active or background state

➡ an active application resigns active state...

‣ on incoming phone call/sms or calendar event

‣ when user presses the Sleep/Wake button while running

‣ when user presses the Home button (if background is supported)

Sunday, February 26, 2012

Page 7: iPHONE iOS4 / SDK4.0 multitasking supportlabs.tranquilli.org/docs/iOS4_background_tranquilli.pdf · Nico Tranquilli • July 2010 “iOS4 multitasking development notes” iPHONE

www.tranquilli.orgNico Tranquilli • July 2010 “iOS4 multitasking development notes”

ENTERING BACKGROUND...

applicationDidEnterBackground is called on app.delegate (and UIApplicationDidEnterBackgroundNotification is posted to registered objects)

➡ application is now in the background

➡ system data objects (cache, etc) are automatically released

➡ you should always save user data and state information here (so you can restore it later, just in case it gets terminated)

➡ you should release as much memory as possible to prevent termination

➡ just five seconds to perform any tasks and return (additional time can be requested) before being suspended

Sunday, February 26, 2012

Page 8: iPHONE iOS4 / SDK4.0 multitasking supportlabs.tranquilli.org/docs/iOS4_background_tranquilli.pdf · Nico Tranquilli • July 2010 “iOS4 multitasking development notes” iPHONE

www.tranquilli.orgNico Tranquilli • July 2010 “iOS4 multitasking development notes”

ACCEPT ?

applicationWillResignActive:

BACKGROUND

USERPRESSES

HOME BUTTON

INTERRUPT(incoming call,

sms, event)

applicationDidEnterBackground:

YES NO

USER GETS NOTIFIED

register for bg tasks ask for exec. time schedule local notif. suspend

ACTIVE

save user data save application state …

INACTIVE

Sunday, February 26, 2012

Page 9: iPHONE iOS4 / SDK4.0 multitasking supportlabs.tranquilli.org/docs/iOS4_background_tranquilli.pdf · Nico Tranquilli • July 2010 “iOS4 multitasking development notes” iPHONE

www.tranquilli.orgNico Tranquilli • July 2010 “iOS4 multitasking development notes”

SAVE DATA BEFORE ENTERING BACKGROUND!

apps running in the background still get incoming messages delivered: observe low-memory warnings as usual

applicationDidReceiveMemoryWarning: (in your app.delegate’s)didReceiveMemoryWarning (in UIViewController)UIApplicationDidReceiveMemoryWarningNotification

when suspended, it won’t get any notice in case of termination: save data and context beforehand!

may be purged from memory and terminated during low-memory conditions: always save context and release as much memory as possibile to prevent termination before moving to the background

applicationWillTerminate gets called as usual if the application is currently runningno termination notice is delivered if the application is suspended

Sunday, February 26, 2012

Page 10: iPHONE iOS4 / SDK4.0 multitasking supportlabs.tranquilli.org/docs/iOS4_background_tranquilli.pdf · Nico Tranquilli • July 2010 “iOS4 multitasking development notes” iPHONE

www.tranquilli.orgNico Tranquilli • July 2010 “iOS4 multitasking development notes”

postpone any UI updates

NEVER make OpenGL ES calls

not use system shared resources (contacs db, etc)

BACKGROUND APPLICATIONS SHOULD…

did I tell you to save state and user data when entering background ?

Sunday, February 26, 2012

Page 11: iPHONE iOS4 / SDK4.0 multitasking supportlabs.tranquilli.org/docs/iOS4_background_tranquilli.pdf · Nico Tranquilli • July 2010 “iOS4 multitasking development notes” iPHONE

www.tranquilli.orgNico Tranquilli • July 2010 “iOS4 multitasking development notes”

BACKGROUND APPLICATIONS CAN…

continue to run, ie. request permission to run in the background and provide an expiration handler (for final cleanup, etc) to be called when your time limit is reached. If your job requires the thread for a long time it makes sense to schedule the work on a dispatch queue so that the run loop is not blocked.

respond to registered background events

schedule local notifications (sound, alerts, badges)

SUSPEND ;-)

Sunday, February 26, 2012

Page 12: iPHONE iOS4 / SDK4.0 multitasking supportlabs.tranquilli.org/docs/iOS4_background_tranquilli.pdf · Nico Tranquilli • July 2010 “iOS4 multitasking development notes” iPHONE

www.tranquilli.orgNico Tranquilli • July 2010 “iOS4 multitasking development notes”

RUNNING IN THE BACKGROUND

background applications have a limited amount of execution time, except for certain (permitted) background tasks

you should wrap any long-running (critical) tasks with beginBackgroundTaskWithExpirationHandler: and endBackgroundTask: calls

‣ you can do this while the app is still in foreground

‣ you can start any bg task at quit time (in your didEnterBackground delegate)

‣ you can start any task later when your app is woken up by a registered bg event

‣ limit for task completion is 600secs. Time left to run is in the backgroundTimeRemaining property of UIApplication

always provide an expiration handler for each task and call the endBackgroundTask: from there (application gets terminated rather then suspended if you leave outstanding background tasks!)

Sunday, February 26, 2012

Page 13: iPHONE iOS4 / SDK4.0 multitasking supportlabs.tranquilli.org/docs/iOS4_background_tranquilli.pdf · Nico Tranquilli • July 2010 “iOS4 multitasking development notes” iPHONE

www.tranquilli.orgNico Tranquilli • July 2010 “iOS4 multitasking development notes”

SOME BACKGROUND TYPES ARE DECLARED

Support for some types of background execution must be declared in advance including the UIBackgroundModes key in your Info.plist. Possible strings in its array value:

audio

location – for continuos, high-accuracy, location updates (drains batteries!). Do not use this if all you need are Significant Location Change notifications!

voip – VoIp applications are automatically started in the background upon system boot, suspended, woken up by the system on incoming connection (socket handed over)

Sunday, February 26, 2012

Page 14: iPHONE iOS4 / SDK4.0 multitasking supportlabs.tranquilli.org/docs/iOS4_background_tranquilli.pdf · Nico Tranquilli • July 2010 “iOS4 multitasking development notes” iPHONE

www.tranquilli.orgNico Tranquilli • July 2010 “iOS4 multitasking development notes”

BACKGROUND LOCATION

significant location change: suspended application is woken up (or relaunched if not running) on significant location changes. Does NOT need to be declared as a background application. Low-power, recommended

standard location updates: location updates are delivered (as usual) to a running fg/bg application. The application gets suspended once background time expires and nothing is delivered when application is suspended or terminated

continuous background location: an application registered as a continuos background location app is never suspended by the system and gets continuous location updates in the backgound (turn-by-turn apps and the like). Power-intensive!

Sunday, February 26, 2012

Page 15: iPHONE iOS4 / SDK4.0 multitasking supportlabs.tranquilli.org/docs/iOS4_background_tranquilli.pdf · Nico Tranquilli • July 2010 “iOS4 multitasking development notes” iPHONE

www.tranquilli.orgNico Tranquilli • July 2010 “iOS4 multitasking development notes”

EARLIER iOS SUPPORT

set the deployment target to the lowest you want to support

set the base and active SDK to the latest stable available

weakly link frameworks that may not exist on the deployment platform

at runtime, make sure code that needs OS 4.0 features (including C blocks) doesn't get executed when running on earlier OS versions: NSClassFromString, [UIDevice currentDevice].multitaskingSupported, respondsToSelector , instancesRespondesToSelector are your friends

Sunday, February 26, 2012

Page 16: iPHONE iOS4 / SDK4.0 multitasking supportlabs.tranquilli.org/docs/iOS4_background_tranquilli.pdf · Nico Tranquilli • July 2010 “iOS4 multitasking development notes” iPHONE

www.tranquilli.orgNico Tranquilli • July 2010 “iOS4 multitasking development notes”

SOME CODE SNIPPETS

-(BOOL)isMultitaskingOS{ BOOL bgSupport=NO; if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])! ! bgSupport=[UIDevice currentDevice].multitaskingSupported; return bgSupport;}

checking multitasking availability

-(BOOL)isForeground{! if (![self isMultitaskingOS])! ! return YES;! UIApplicationState state = [UIApplication sharedApplication].applicationState;! //return (state==UIApplicationStateActive || state==UIApplicationStateInactive );! return (state==UIApplicationStateActive);}

checking the runtime state of the app

Sunday, February 26, 2012

Page 17: iPHONE iOS4 / SDK4.0 multitasking supportlabs.tranquilli.org/docs/iOS4_background_tranquilli.pdf · Nico Tranquilli • July 2010 “iOS4 multitasking development notes” iPHONE

www.tranquilli.orgNico Tranquilli • July 2010 “iOS4 multitasking development notes”

SOME CODE SNIPPETS

UIApplication *app=[UIApplication sharedApplication];NSAssert(self->bgTask == UIInvalidBackgroundTask, nil);// beginBackgroundTaskWithExpirationHandler: marks the beginning of // a new long-running background task.bgTask = [app beginBackgroundTaskWithExpirationHandler: ^{! dispatch_async(dispatch_get_main_queue(), ^{! ! [app endBackgroundTask:self->bgTask];! ! self->bgTask = UIInvalidBackgroundTask;! });}];!dispatch_async(dispatch_get_main_queue(), ^{! // do something!! // eg:while ([app backgroundTimeRemaining] > 1.0) { ; }! !! [app endBackgroundTask:self->bgTask];! self->bgTask = UIInvalidBackgroundTask;});

making sure an app is not suspended while completing a task in the background (uses C blocks and the Grand Central Dispatch)

Sunday, February 26, 2012

Page 18: iPHONE iOS4 / SDK4.0 multitasking supportlabs.tranquilli.org/docs/iOS4_background_tranquilli.pdf · Nico Tranquilli • July 2010 “iOS4 multitasking development notes” iPHONE

www.tranquilli.orgNico Tranquilli • July 2010 “iOS4 multitasking development notes”

SOME CODE SNIPPETS

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ ! // ....

if ([launchOptions objectForKey:@"UIApplicationLaunchOptionsLocationKey"]]) { // application was (re)launched in response to an incoming location event! }

! // ....!}

background app relaunch by os due to a location event

Sunday, February 26, 2012

Page 19: iPHONE iOS4 / SDK4.0 multitasking supportlabs.tranquilli.org/docs/iOS4_background_tranquilli.pdf · Nico Tranquilli • July 2010 “iOS4 multitasking development notes” iPHONE

www.tranquilli.orgNico Tranquilli • July 2010 “iOS4 multitasking development notes”

iPHONE iOS4 / SDK4.0 multitasking support

from a developer’s perspective

Nico [email protected]

PlaceTrack has been available on the App Store since 09/2010

Sunday, February 26, 2012