ios - 如何使用 beginBackgrounTaskWithExpirationHandler 有选择地指定在后台执行的内容 :

标签 ios

我正在查看 Sam 的 Teach Yourself iPhone 开发版,但我不明白给出的背景示例。非后台代码是:

  - (void)viewDidLoad {
       [super viewDidLoad];
       count=0;
       theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1
                       target:self
                       selector:@selector(countUp)
                       userInfo:nil
                       repeats:YES];
   }

后台版本是:

- (void)viewDidLoad {
    [super viewDidLoad];

    counterTask = [[UIApplication sharedApplication]
              beginBackgroundTaskWithExpirationHandler:^{
                 // If you're worried about exceeding 10 minutes, handle it here
              }];
    count=0;
    theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1
                    target:self
                    selector:@selector(countUp)
                    userInfo:nil
                    repeats:YES];
}

我不明白的是要在后台执行的事件和 beginBackgroundTaskWithExpirationHandler 之间的关联在哪里。

在此示例中,NSTimer 在后台运行 - 但如果还有其他一些事件要在后台执行,即假设代码是:

- (void)viewDidLoad {
    [super viewDidLoad];

    counterTask = [[UIApplication sharedApplication]
              beginBackgroundTaskWithExpirationHandler:^{
                 // If you're worried about exceeding 10 minutes, handle it here
              }];
    count=0;
    theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1
                    target:self
                    selector:@selector(countUp)
                    userInfo:nil
                    repeats:YES];

  x: some other activity to be performed in the background
  maybe another timer with a difference time interval
}

如何另外指定 x 也在后台执行?

或者我是否误解了它的工作原理,当调用 beginBackgrounTaskWithExpirationHandler 时,它实际上是在后台执行的整个应用程序?如果是这种情况,那么为什么需要任务标识符,因为您只能启动一个任务,即您的应用程序?

如果情况并非如此,并且可以选择不同的任务在后台执行,那么这是如何实现的?假设在此示例中 X 是具有不同间隔和不同到期条件的第二个计时器,如果我希望 theTimer 和 x 在后台执行,代码会是什么样子?换句话说,如果代码是这样的怎么办:

- (void)viewDidLoad {
    [super viewDidLoad];

    counterTask = [[UIApplication sharedApplication]
              beginBackgroundTaskWithExpirationHandler:^{
                 // If you're worried about exceeding 10 minutes, handle it here
              }];
    count=0;
    theTimer=[NSTimer scheduledTimerWithTimeInterval:0.1
                    target:self
                    selector:@selector(countUp)
                    userInfo:nil
                    repeats:YES];

    theTimer2=[NSTimer scheduledTimerWithTimeInterval:1.0
                    target:self
                    selector:@selector(aDifferentMethod)
                    userInfo:nil
                    repeats:YES];

    theTimer3=[NSTimer scheduledTimerWithTimeInterval:10.0
                    target:self
                    selector:@selector(anotherDifferentMethod)
                    userInfo:nil
                    repeats:YES];

}

如何指定 theTimer 和 theTimer2 都在后台执行,而 theTimer3 不在后台执行?

最佳答案

Or have I misunderstood how it works and when beginBackgrounTaskWithExpirationHandler is called it is the the whole of the application whcih will in fact execute in the background? If that is the case then why is a task identifier necessary as you would only be able to start one task which is your app?

整个应用程序将运行。做什么或不做什么由你决定。 beginBackgroundTaskWithExpirationHandler: 的要点是它告诉系统您有一些长时间运行的事情要做。它会做一个记录,并返还给你一个 token 。当长时间运行的任务完成后,您可以使用该 token 调用 endBackgroundTask:。有可能在那段时间里,你从未真正进入过后台。

当您的应用程序将被暂停时,系统会查看您是否有任何待处理的 token 。如果你不这样做,那么它只会让你暂停。如果你这样做了,那么它可以让你再跑一段时间。系统并不关心您如何将这些 token 与工作关联起来。您可以为整个程序使用一个 token 。或者每个对象在需要做某事时可以保留自己的 token 。重点是,只要您有一个 token ,您通常就会在被暂停之前获得一些额外的工作时间。

对于有关计时器的问题,您通常不应在后台执行任何计时器工作。你的时间不多(最多10分钟)。你需要做完你的工作然后出去。当您进入后台时,您应该暂停(无效)所有计时器。然后,您可以在恢复时将它们重新设置。

关于ios - 如何使用 beginBackgrounTaskWithExpirationHandler 有选择地指定在后台执行的内容 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9509827/

相关文章:

ios - 从 Firebase 检索子值

ios - Xcode 5不显示方案选项以在iOS 6上运行模拟器

ios - Xcode 9 iOS 11 模拟器 Touch ID 不工作

ios - 背景颜色错误的状态栏

ios - 同时更改所有表格 View 单元格上的显示参数

ios - 在 Swift 中持续更新 UILabel

ios - tableViewCell 中的约束未正确显示

ios - 如何在点击 "Add"或 "Cancel"按钮后关闭 EKEventEditViewController

ios - 注册来自 collabd 的消息,例如 XCSBuildService 以接收 Xcode Bots 集成号

ios - 从 UIWebView 获取加载的图片