跟上一篇同样,这篇也主要作一些要点记录html
一、app终止也看成app正常生命周期的一部分,若是app是被挂起的,是接收不到被终止的消息的。若是app正在运行或者app在后台但没被挂起,appdelegate是会接收到applicationWillTerminate:通知的。无论是用户仍是系统要杀,通知行为是同样的。ios
二、应该启动的时候当任务关系到构造正确的界面时才应该在主线程执行,其它任务都应该异步执行。app
- (void)applicationDidEnterBackground:(UIApplication *)application { bgTask = [application beginBackgroundTaskWithName:@"MyTask" expirationHandler:^{ // Clean up any unfinished task business by marking where you // stopped or ending the task outright. [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; // Start the long-running task and return immediately. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Do the work associated with the task, preferably in chunks. [application endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }); }
但注意的是不管时本身任务执行完毕或者系统告诉你申请的时间已经到了的时候必定要调endBackgroundTask:告诉系统你能够被挂起了,无论你是否完成了你所作的任务。不然就会被系统杀掉。也能够经过backgroundTimeRemaining知道申请的时间剩余多少。异步
(2) 对于后台下载可参考NSURLSession。但有些东西仍是有必要了解和备注:若是是系统挂起或者说杀死app,它才会继续NSURLSession的后台任务,若是是用户主动杀掉应用,系统会取消掉全部未完成的下载任务。async
application:performFetchWithCompletionHandler:,它会平衡全部其它应用才决定要不要调你的。这里能够用做检查一些更新,配合
NSURLSession的后台下载实现一些更好的功能。