正确使用 beginBackgroundTaskWithExpirationHandler [英] Proper use of beginBackgroundTaskWithExpirationHandler

查看:90
本文介绍了正确使用 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: URLResponse?, 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.shared.beginBackgroundTask(expirationHandler: ({}))
}

func endBackgroundUpdateTask(taskID: UIBackgroundTaskIdentifier) {
    UIApplication.shared.endBackgroundTask(taskID)
}

这篇关于正确使用 beginBackgroundTaskWithExpirationHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆