我有一些長時間運行的進程,即使應用程序在後臺運行,我也想運行它。我正在調用應用程序的beginBackgroundTaskWithExpirationHandler:
方法,並在expirationBlock中調用應用程序的endBackgroundTask
。 這裏是實現:beginBackgroundTaskWithExpirationHandler調用endBackgroundTask但沒有結束過程
__block UIBackgroundTaskIdentifier task = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:task];
task = UIBackgroundTaskInvalid;
}];
dispatch_queue_t queue = dispatch_queue_create("com.test.test1234", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
// My Task goes here
});
在某些情況下,我的序列隊列有更多的任務要執行,不能由制度規定的時間內完成。所以到期塊將執行,並在那我結束UIBackgroundTaskIdentifier
但不停止調度過程(我甚至不能取消派遣)。
蘋果的文件說:
每次調用beginBackgroundTaskWithName:expirationHandler:或beginBackgroundTaskWithExpirationHandler:方法生成一個獨特的標記與相應的任務關聯起來。當您的應用完成一項任務時,必須使用相應的令牌調用endBackgroundTask:方法,以讓系統知道任務已完成。未能調用後臺任務的endBackgroundTask:方法將導致應用程序終止。如果您在啓動任務時提供了到期處理程序,系統會調用該處理程序,併爲您提供最後一次結束任務並避免終止的機會。
所以,根據這個如果我不叫endBackgroundTask:
我的應用程序將被終止,這是可以的。
我的問題是:在我目前的實施中,如果我在expirationHandler塊中調用endBackgroundTask:
,並且我的調度隊列的任務沒有完成,該怎麼辦?我的應用程序將被終止或將被暫停?
感謝
如果你叫做endBackgroundTask:在任何你的隊列裏。它只是暫停你的應用程序,讓應用程序進入睡眠。 –
@chiragshah所以這不能成爲看門狗殺死我的應用程序的權利?只有當我錯過'endBackgroundTask'時,看門狗才會殺死應用程序? –
是的,我做了同樣的想法,在我的應用程序,它可以正常工作 –