浅谈iOS的多任务机制
对320的程序持久化很感兴趣,于是首先研究了下iOS的多任务机制.对于大多数的应用,如果不是特别需要,用最新的SDK编译出来的程序本身就是支持多任务的—按home键程序进入后台运行(但是注意此时的程序并不是会运行,只是进入后台状态便于其再次进入活动状态,这一点同我们概念中应该有的多任务有区别).而对于一些应用,是需充分运用iOS多任务的特性,如游戏应用和需要网络连接的应用等等.
(转载请注明 www.cnblogs.com/scorpiozj)
多任务机制是苹果在iOS4中引进的,我们首先新建一个工程,查看下appdelegate文件中的内容:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
- (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:.
*/
}
以上的函数就是ios4中引入的用于支持多任务运行的函数.从函数名我们就可以猜测到程序可能的一些状态:background,active. 图1就是程序的生命周期图.
图1
我们来看一下:程序第一次启动,从Not Running状态进入active阶段,程序会调用两个函数:
- didFinishLaunchingWithOptions:和
- applicationDidBecomeActive:
此时点击home键,程序就会进入后台直至进入挂起状态,程序会依次调用下述函数:
- applicationWillResignActive:
- applicationDidEnterBackground:
这时双击home键再进入该程序时,程序会依次调用:
- applicationWillEnterForeground:
- applicationDidBecomeActive:
注意到Background包含running和suspend两种状态.这里的running不是真正意义上的程序运行,而是指的applicationDidEnterBackground:运行部分,通常情况下,系统给此函数执行的时间不会太多,并且执行完后程序就在后台挂起.这就是iOS多任务的绝大部分情形,但是等一下,就三种情况苹果是允许你在后台运行的
- 音乐
- 位置
- VoIP
你可以在程序中的plist文件中进行设置,如图2:
图2
可以这么说,除了这三种服务允许以我们通常认为的多任务机制在后台运行,其他的程序的多任务,就是上文所指的程序状态.这里不讨论为啥苹果要这样做,只是稍微说明一下,在这种情况下,至少是在表面上实现了多任务:比如说我玩了愤怒的小鸟又想看下天气,然后又想玩小鸟了,这时切换就比较容易,因为程序这时是在后台挂起,还是在内存中运行着的,这样再启动时就会比较快.
程序调用相应函数的时候,系统会发送相应的Notification,这时app就应该适时的保存app状态或是读取app的历史状态,这样才能更好的呈现用户体验.
之前说过,程序进入后台的时间很短,有时候app要做的操作还没来得及进行.这时,我们可以使用beginBackgroundTaskWithExpirationHandler:来处理耗时可能比较长的操作.
总结一下:
ios所谓的多任务并不是我们通常以为的多任务,ios只允许三种服务在后台运行;其他的只是方便多个app之间的切换.
(至于如何实现多任务,比如进入后台程序应该优先进行哪些操作的细节,会在以后结合本人的实际加以说明.)