objective c - 正确使用beginBackgroundTaskWithExpirationHandler [英] objective c - Proper use of beginBackgroundTaskWithExpirationHandler
问题描述
我对如何以及何时使用 beginBackgroundTaskWithExpirationHandler
感到有点困惑。
I'm a bit confused about how and when to use beginBackgroundTaskWithExpirationHandler
.
Apple在他们的示例中显示在 applicationDidEnterBackground
委托中使用它,以获得更多时间来完成一些重要任务,通常是网络交易。
Apple shows in their examples to use it in applicationDidEnterBackground
delegate, to get more time to complete some important task, usually a network transaction.
在查看我的应用程序时,似乎我的大多数网络内容都很重要,如果用户按下主页按钮,我想完成它。
When looking on my app, it seems like most of my network stuff is important, and when one is started I would like to complete it if the user pressed the home button.
因此,包含 beginBackgroundTaskWithExpirationHandler
的每个网络事务(并且我不是在谈论下载大块数据,大多数是一些短xml)是接受/良好做法在安全方面?
So is it accepted/good practice to wrap every network transaction (and I'm not talking about downloading big chunk of data, it mostly some short xml) with beginBackgroundTaskWithExpirationHandler
to be on the safe side?
推荐答案
如果您希望您的网络事务在后台继续,那么您需要将其包装在后台任务中。完成后调用 endBackgroundTask
也非常重要 - 否则应用程序将在其分配的时间到期后被杀死。
If you want your network transaction to continue in the background, then you'll need to wrap it in a background task. It's also very important that you call endBackgroundTask
when you're finished - otherwise the app will be killed after its allotted time has expired.
我看起来像这样:
- (void) doUpdate
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self beginBackgroundUpdateTask];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * responseData = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];
// Do something with the result
[self endBackgroundUpdateTask];
});
}
- (void) beginBackgroundUpdateTask
{
self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundUpdateTask];
}];
}
- (void) endBackgroundUpdateTask
{
[[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];
self.backgroundUpdateTask = UIBackgroundTaskInvalid;
}
我有一个 UIBackgroundTaskIdentifier
每个后台任务的属性
I have a UIBackgroundTaskIdentifier
property for each background task
Swift中的等效代码
Equivalent code in Swift
func doUpdate () {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
let taskID = beginBackgroundUpdateTask()
var response: NSURLResponse?, error: NSError?, request: NSURLRequest?
let data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)
// Do something with the result
endBackgroundUpdateTask(taskID)
})
}
func beginBackgroundUpdateTask() -> UIBackgroundTaskIdentifier {
return UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({})
}
func endBackgroundUpdateTask(taskID: UIBackgroundTaskIdentifier) {
UIApplication.sharedApplication().endBackgroundTask(taskID)
}
这篇关于objective c - 正确使用beginBackgroundTaskWithExpirationHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!