詳解IOS多任務(wù)機(jī)制 初學(xué)者必看文檔
IOS多任務(wù)機(jī)制是本文要介紹的內(nèi)容,對IOS程序持久化很感興趣,于是首先研究了下iOS的多任務(wù)機(jī)制.對于大多數(shù)的應(yīng)用,如果不是特別需要,用***的SDK編譯出來的程序本身就是支持多任務(wù)的—按home鍵程序進(jìn)入后臺運(yùn)行(但是注意此時的程序并不是會運(yùn)行,只是進(jìn)入后臺狀態(tài)便于其再次進(jìn)入活動狀態(tài),這一點(diǎn)同我們概念中應(yīng)該有的多任務(wù)有區(qū)別).而對于一些應(yīng)用,是需充分運(yùn)用iOS多任務(wù)的特性,如游戲應(yīng)用和需要網(wǎng)絡(luò)連接的應(yīng)用等等。
多任務(wù)機(jī)制是蘋果在iOS 4中引進(jìn)的,我們首先新建一個工程,查看下appdelegate文件中的內(nèi)容:
- App cycle
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- { /
- / Override point for customization after application launch.
- // Set the view controller as the window's root view controller and display. return YES;
- }
- - (void)applicationWillResignActive:(UIApplication *)application
- {
- /* Sent when the application is about to move from active to inactive state.
- This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or
- when the user quits the application and it begins the transition to the background state.
- Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates.
- Games should use this method to pause the game.
- */
- }
- - (void)applicationDidEnterBackground:(UIApplication *)application
- {
- /* Use this method to release shared resources, save user data, invalidate timers,
- and store enough application state information to restore your application to its current state in case it is terminated later.
- If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
- */
- }
- - (void)applicationWillEnterForeground:(UIApplication *)application
- {
- /*
- Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
- */
- }
- - (void)applicationDidBecomeActive:(UIApplication *)application
- {
- /* Restart any tasks that were paused (or not yet started) while the application was inactive.
- If the application was previously in the background, optionally refresh the user interface.
- */
- }
- - (void)applicationWillTerminate:(UIApplication *)application
- {
- /* Called when the application is about to terminate.
- See also applicationDidEnterBackground:.
- */
- }
以上的函數(shù)就是ios4中引入的用于支持多任務(wù)運(yùn)行的函數(shù).從函數(shù)名我們就可以猜測到程序可能的一些狀態(tài):background,active. 圖1就是程序的生命周期圖.
圖1
我們來看一下:程序***次啟動,從Not Running狀態(tài)進(jìn)入active階段,程序會調(diào)用兩個函數(shù):
- didFinishLaunchingWithOptions:和
- applicationDidBecomeActive:
此時點(diǎn)擊home鍵,程序就會進(jìn)入后臺直至進(jìn)入掛起狀態(tài),程序會依次調(diào)用下述函數(shù):
- applicationWillResignActive:
- applicationDidEnterBackground:
這時雙擊home鍵再進(jìn)入該程序時,程序會依次調(diào)用:
- applicationWillEnterForeground:
- applicationDidBecomeActive:
注意到Background包含running和suspend兩種狀態(tài).這里的running不是真正意義上的程序運(yùn)行,而是指的applicationDidEnterBackground:運(yùn)行部分,通常情況下,系統(tǒng)給此函數(shù)執(zhí)行的時間不會太多,并且執(zhí)行完后程序就在后臺掛起.這就是iOS多任務(wù)的絕大部分情形,但是等一下,就三種情況蘋果是允許你在后臺運(yùn)行的
音樂
位置
VoIP
你可以在程序中的plist文件中進(jìn)行設(shè)置,如圖2:
圖2
可以這么說,除了這三種服務(wù)允許以我們通常認(rèn)為的多任務(wù)機(jī)制在后臺運(yùn)行,其他的程序的多任務(wù),就是上文所指的程序狀態(tài).這里不討論為啥蘋果要這樣做,只是稍微說明一下,在這種情況下,至少是在表面上實(shí)現(xiàn)了多任務(wù):比如說我玩了憤怒的小鳥又想看下天氣,然后又想玩小鳥了,這時切換就比較容易,因?yàn)槌绦蜻@時是在后臺掛起,還是在內(nèi)存中運(yùn)行著的,這樣再啟動時就會比較快.
程序調(diào)用相應(yīng)函數(shù)的時候,系統(tǒng)會發(fā)送相應(yīng)的Notification,這時app就應(yīng)該適時的保存app狀態(tài)或是讀取app的歷史狀態(tài),這樣才能更好的呈現(xiàn)用戶體驗(yàn),之前說過,程序進(jìn)入后臺的時間很短,有時候app要做的操作還沒來得及進(jìn)行.這時,我們可以使用beginBackgroundTaskWithExpirationHandler:來處理耗時可能比較長的操作.
iOS所謂的多任務(wù)并不是我們通常以為的多任務(wù),IOS只允許三種服務(wù)在后臺運(yùn)行;其他的只是方便多個app之間的切換.,至于如何實(shí)現(xiàn)多任務(wù),比如進(jìn)入后臺程序應(yīng)該優(yōu)先進(jìn)行哪些操作的細(xì)節(jié),會在以后結(jié)合本人的實(shí)際加以說明。
小結(jié):詳解iOS多任務(wù)機(jī)制 初學(xué)者必看文檔的內(nèi)容介紹完了,希望本文讀你有所幫助!