詳解IOS多任務(wù)機(jī)制 初學(xué)者必看文檔
IOS多任務(wù)機(jī)制是本文要介紹的內(nèi)容,對(duì)IOS程序持久化很感興趣,于是首先研究了下iOS的多任務(wù)機(jī)制.對(duì)于大多數(shù)的應(yīng)用,如果不是特別需要,用***的SDK編譯出來(lái)的程序本身就是支持多任務(wù)的—按home鍵程序進(jìn)入后臺(tái)運(yùn)行(但是注意此時(shí)的程序并不是會(huì)運(yùn)行,只是進(jìn)入后臺(tái)狀態(tài)便于其再次進(jìn)入活動(dòng)狀態(tài),這一點(diǎn)同我們概念中應(yīng)該有的多任務(wù)有區(qū)別).而對(duì)于一些應(yīng)用,是需充分運(yùn)用iOS多任務(wù)的特性,如游戲應(yīng)用和需要網(wǎng)絡(luò)連接的應(yīng)用等等。
多任務(wù)機(jī)制是蘋(píng)果在iOS 4中引進(jìn)的,我們首先新建一個(gè)工程,查看下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ù)名我們就可以猜測(cè)到程序可能的一些狀態(tài):background,active. 圖1就是程序的生命周期圖.
圖1
我們來(lái)看一下:程序***次啟動(dòng),從Not Running狀態(tài)進(jìn)入active階段,程序會(huì)調(diào)用兩個(gè)函數(shù):
- didFinishLaunchingWithOptions:和
- applicationDidBecomeActive:
此時(shí)點(diǎn)擊home鍵,程序就會(huì)進(jìn)入后臺(tái)直至進(jìn)入掛起狀態(tài),程序會(huì)依次調(diào)用下述函數(shù):
- applicationWillResignActive:
- applicationDidEnterBackground:
這時(shí)雙擊home鍵再進(jìn)入該程序時(shí),程序會(huì)依次調(diào)用:
- applicationWillEnterForeground:
- applicationDidBecomeActive:
注意到Background包含running和suspend兩種狀態(tài).這里的running不是真正意義上的程序運(yùn)行,而是指的applicationDidEnterBackground:運(yùn)行部分,通常情況下,系統(tǒng)給此函數(shù)執(zhí)行的時(shí)間不會(huì)太多,并且執(zhí)行完后程序就在后臺(tái)掛起.這就是iOS多任務(wù)的絕大部分情形,但是等一下,就三種情況蘋(píng)果是允許你在后臺(tái)運(yùn)行的
音樂(lè)
位置
VoIP
你可以在程序中的plist文件中進(jìn)行設(shè)置,如圖2:
圖2
可以這么說(shuō),除了這三種服務(wù)允許以我們通常認(rèn)為的多任務(wù)機(jī)制在后臺(tái)運(yùn)行,其他的程序的多任務(wù),就是上文所指的程序狀態(tài).這里不討論為啥蘋(píng)果要這樣做,只是稍微說(shuō)明一下,在這種情況下,至少是在表面上實(shí)現(xiàn)了多任務(wù):比如說(shuō)我玩了憤怒的小鳥(niǎo)又想看下天氣,然后又想玩小鳥(niǎo)了,這時(shí)切換就比較容易,因?yàn)槌绦蜻@時(shí)是在后臺(tái)掛起,還是在內(nèi)存中運(yùn)行著的,這樣再啟動(dòng)時(shí)就會(huì)比較快.
程序調(diào)用相應(yīng)函數(shù)的時(shí)候,系統(tǒng)會(huì)發(fā)送相應(yīng)的Notification,這時(shí)app就應(yīng)該適時(shí)的保存app狀態(tài)或是讀取app的歷史狀態(tài),這樣才能更好的呈現(xiàn)用戶體驗(yàn),之前說(shuō)過(guò),程序進(jìn)入后臺(tái)的時(shí)間很短,有時(shí)候app要做的操作還沒(méi)來(lái)得及進(jìn)行.這時(shí),我們可以使用beginBackgroundTaskWithExpirationHandler:來(lái)處理耗時(shí)可能比較長(zhǎng)的操作.
iOS所謂的多任務(wù)并不是我們通常以為的多任務(wù),IOS只允許三種服務(wù)在后臺(tái)運(yùn)行;其他的只是方便多個(gè)app之間的切換.,至于如何實(shí)現(xiàn)多任務(wù),比如進(jìn)入后臺(tái)程序應(yīng)該優(yōu)先進(jìn)行哪些操作的細(xì)節(jié),會(huì)在以后結(jié)合本人的實(shí)際加以說(shuō)明。
小結(jié):詳解iOS多任務(wù)機(jī)制 初學(xué)者必看文檔的內(nèi)容介紹完了,希望本文讀你有所幫助!