如何在iOS应用程序中使用多任务处理?

发布于 2024-10-11 18:48:13 字数 636 浏览 3 评论 0原文

对iOS中的多任务API感到非常困惑,例如beginBackgroundTaskWithExpirationHandler等。

例如录制视频,当按下按钮开始录制时,我们输入

 if ([[UIDevice currentDevice] isMultitaskingSupported]) {
        [self setBackgroundRecordingID:[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{}]];
    }

录制结束时间

if ([[UIDevice currentDevice] isMultitaskingSupported]) {
        [[UIApplication sharedApplication] endBackgroundTask:[self backgroundRecordingID]];
    }

,谁能解释一下这些是什么意思。 正如你所看到的,当开始录制时,块beginBackgroundTaskWithExpirationHandler是空白的(只是无事可做?),这些代码的用途是什么?

非常感谢您的解释。

Really confused about multitasking API in iOS, such as beginBackgroundTaskWithExpirationHandler etc.

For instance of recording video, when press button to start recording, we put

 if ([[UIDevice currentDevice] isMultitaskingSupported]) {
        [self setBackgroundRecordingID:[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{}]];
    }

when recording ends

if ([[UIDevice currentDevice] isMultitaskingSupported]) {
        [[UIApplication sharedApplication] endBackgroundTask:[self backgroundRecordingID]];
    }

so who can explain what is meaning of these.
As you can see, when start recording, the block beginBackgroundTaskWithExpirationHandler is blank( just nothing to do ? ), and what is usage of those codes ?

Pretty much thanks for explaining.

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

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

发布评论

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

评论(2

饮湿 2024-10-18 18:48:13

这些调用的目的是让您的应用程序进入后台,而不会像通常那样被挂起。他们要求系统给予您的应用一些额外的生存时间,即使它已经进入后台。

您想要在后台执行的操作是 beginBackgroundTask...endBackgroundTask: 之间的任何事情。重要的是,在使用 beginBackgroundTask... 宣布冗长任务开始后,您必须随后调用 endBackgroundTask: 让系统知道您已完成,并且已完成现在可以让你停职了。无论您的代码采用什么路线,您都必须始终平衡这些调用。一定要涵盖所有情况!

过期处理程序不是您想要在后台执行的操作;正如 FX 所说,它是一个错误处理程序。当您超出后台时间限制(通常为十分钟)的严重紧急情况下,它将被调用。如果它被调用,这意味着系统将立即暂停您,并且您无法阻止它。

你永远不应该有一个空的过期处理程序!原因是,如果你达到了时间限制并且你的过期处理程序实际上被调用了,如果它没有调用 endBackgroundTask:,系统不仅会强行暂停您的应用程序,还会杀死您的应用程序!因此,您的过期处理程序必须始终非常快速地执行,并且必须至少包含对 endBackgroundTask: 的调用。

The purpose of these calls is to let your app go into the background without being suspended as it normally would be. They request that the system grant your app some extra time to live, even though it has been backgrounded.

The thing you want to do in the background is whatever lies between beginBackgroundTask... and endBackgroundTask:. The important thing is that having announced the start of your lengthy task with beginBackgroundTask... you must subsequently call endBackgroundTask: to let the system know you're done and it is now okay to suspend you. You must always balance these calls, no matter what route your code takes. Be sure to cover all situations!

The expiration handler is not the thing you want to do in the background; it is, as FX rightly says, an error handler. It will be called in a dire emergency where you've exceeded your background time limit (nominally ten minutes). If it is called, this means that the system is going to suspend you right now and you can't stop it.

You should never have an empty expiration handler! The reason is that if you hit your time limit and your expiration handler is actually called, if it does not call endBackgroundTask:, the system will not only forcibly suspend your app, it will kill your app! So, your expiration handler must always execute very quickly and must minimally include a call to endBackgroundTask:.

月下客 2024-10-18 18:48:13

将过期处理程序视为任何错误处理程序。系统只允许后台应用程序使用有限的CPU时间,所以如果达到这个限制,后台操作将被终止,并调用你的处理程序。用它来清理应用程序的内部状态。

Think of the expiration handler as any error handler. The system only allows the background application to use a limited amount of CPU time, so if you reach this limit, the background operation will be terminated, and your handler is called. Use it to clean up the internal state of your application.

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