多个 beginBackgroundTaskWithExpirationHandler

发布于 2024-12-12 17:36:59 字数 1018 浏览 0 评论 0原文

如果我们有一个长时间运行的任务,我们可以使用以下代码确保应用程序在该任务完成之前不会退出:

UIApplication *app = [UIApplication sharedApplication];    

bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    // Clean up any unfinished task business by marking where you.
    // stopped or ending the task outright.
    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
}];

// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

    // Do the work associated with the task, preferably in chunks.

    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
});

在我的例子中,应用程序 plist 包含 Application does not run in 的条目背景设置为YES。现在,我有一个串行队列(dispatch_queue_create(..., NULL)),它记录操作并一一执行它们。用户可以一次发布多个任务,所有任务可能需要 20-30 秒才能完成。

现在我的问题是,我可以将 beginBackgroundTaskWithExpirationHandler 与我的串行队列一起使用吗?如果是这样,有什么建议吗?我相信我必须维护一个 UIBackgroundTaskIdentifier 数组?

If we have a long running task, we can ensure that the application doesn't quit until that task is complete by using the following piece of code:

UIApplication *app = [UIApplication sharedApplication];    

bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    // Clean up any unfinished task business by marking where you.
    // stopped or ending the task outright.
    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
}];

// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{

    // Do the work associated with the task, preferably in chunks.

    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
});

In my case, the applications plist contains an entry for Application does not run in background set to YES. Now, i have a serial queue (dispatch_queue_create(..., NULL)) that logs the operations and perform them one by one. User can post many tasks at a time and it might take couple of seconds 20-30 for all of them to complete.

Now my question is, can i use beginBackgroundTaskWithExpirationHandler with my serial queue? If so, any recommendations as to how? I believe i'll have to maintain an array of UIBackgroundTaskIdentifier?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苄①跕圉湢 2024-12-19 17:36:59

据我所知,如果应用程序不支持后台处理,应用程序将立即退出。因此,启动后台任务是不可能的。

To the best of my knowledge, if the application doesn't support background processing, the application quits immediately. So, starting a background tasks is not possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文