当您的应用程序休眠时,您如何知道任务是否在后台执行?

发布于 2024-12-09 00:16:43 字数 861 浏览 1 评论 0原文

对于 Objective C 内存管理,我们有一个一般规则:如果使用以下任何技术创建对象,则需要稍后释放该对象。

  • alloc
  • new
  • copy
  • mutableCopy

这个规则很容易记住。我正在寻找一个类似的简单规则来决定应用程序睡眠时是否进行后台处理。是否存在这样一个明确的清单?

  • NSUrlRequest - 如果您发送请求然后立即点击主页按钮,我的应用程序是否仍然可以处理服务器响应?
  • 计时器 - 您启动计时器来触发 1从现在起一分钟后,但您在 1 分钟过去之前按下了主页按钮。计时器是否仍在运行、是否暂停或被取消?
  • For 循环 - 您编写一个执行一百万次迭代的 for 循环。您在完成 600,000 次迭代后关闭应用程序。当应用程序休眠以及重新打开应用程序时,其余 400,000 次迭代会发生什么?
  • 等等...

每当应用程序休眠时,我都会想到很多其他事情。我很担心,因为自从我开始开发这个摄像机应用程序以来,我的电池寿命一直很糟糕(一夜之间下降了 50%)。也许这是一个巧合,或者当应用程序处于睡眠状态时相机仍在录制。

With Objective C memory management, we have the general rule that if you create an object with any of the following techniques, you need to release the object later.

  • alloc
  • new
  • copy
  • mutableCopy

This rule is simple to remember. I'm looking for a analogous simple rule for deciding whether something does background processing when the app sleeps. Does such a definitive list exist?

  • NSUrlRequest - if you send a request out then hit the home button right away, is it possible that my app still processes the server response?
  • Timer - you set off a timer to fire 1 minute from now, but you hit the home button before 1 minute elapse. Does the timer still run, does it get paused, or does it get canceled?
  • For loop - you write a for loop that does a million iterations. You close the app after it's done with 600,000 iterations. What happens to the rest of the 400,000 iterations when the app sleeps and when you re-open the app?
  • And so on...

There's a dozen other things I'm thinking about whenever the app sleeps. I'm worried because ever since I started developing this video camera app, my battery life has been horrible (dropping 50% overnight). Maybe it's a coincidence or maybe the camera is still recording while the app is sleeping.

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

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

发布评论

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

评论(1

凯凯我们等你回来 2024-12-16 00:16:43

它在某种程度上取决于您的包中的 UIBackgroundModes 标志。您可以使用它们告诉 iOS 您想要继续在后台运行。您还可以使用 beginBackgroundTaskWithExpirationHandler 动态请求足够的时间来继续下载几分钟。

如果您什么都不做,默认情况下会挂起您的应用程序——NSTimers 在您返回前台之前不会触发。如果您处于主线程的循环中,您的应用程序将获得一点时间来完成它,但未指定确切的时间量。如果主线程上的循环时间过长,即使您位于前台,您的应用程序也会被终止。

我不是 100% 确定 NSURLConnections 会发生什么,但我怀疑它们默认被取消。

请参阅:

iPhoneOS 编程指南 (PDF),第 17 页65

至于你的电池问题,你的应用程序肯定是造成这个问题的原因。例如,GPS 非常耗电,尤其是当您继续在后台运行以播放音频或进行其他处理时。

It depends on the UIBackgroundModes flags in your bundle to some extent. You can use them to tell iOS that you want to continue running in the background. You can also dynamically request enough time to continue a download for a few minutes using beginBackgroundTaskWithExpirationHandler.

If you do nothing, the default is to suspend your app -- NSTimers will not fire until you return to the foreground. If you are in a loop on the main thread, your app will get a little bit of time to finish it, but the exact amount of time is not specified. If a loop on the main thread takes too long, your app will be terminated even if you are in the foreground.

I'm not 100% sure what happens with NSURLConnections, but I suspect they are canceled by default.

See:

iPhoneOS Programming Guide (PDF), p. 65

As for your battery issue, your app could definitely be responsible for that. The GPS is a notorious battery hog, for example, especially if you continue to run in the background to play audio or do other processing.

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