dispatch_after限制为10秒? [英] dispatch_after is limited to 10 seconds?

查看:124
本文介绍了dispatch_after限制为10秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个在后台运行的应用。有时我需要告诉用户发生了什么事情,所以我播放了一定次数的声音。为此我做了一个计时器,但问题是它不能超过总延迟时间的10s,之后,没有更多的声音(但应用程序仍在运行)。我已经在前台模式中检查了这种行为并且它运行良好。

I'm developing an app which is running in background. Sometimes I need to tell the user that something is happenning so I play a sound a certain number of times. To do that I made a timer but the problem is that It cannot exceed 10s of the total delayed time, after that, there are no more sounds (but the app is still running). I've checked this behavior in foreground mode and it works perfectly.

当我需要通知用户时,这是我的代码:

This is my code when I need to inform the user:



    AudioServicesPlaySystemSound(alarma);
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    numAlarm = 1;  
    NSTimeInterval delay_in_seconds = 2.0;
    dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW,delay_in_seconds*NSEC_PER_SEC);
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
    dispatch_after(delay,queue,^{
        [self fiBGTemps:ALARM];
    });

和fiBGTemps是这样的,

and the "fiBGTemps" is something like this,


- (void)fiBGTemps:(int)sender
{
    switch (sender){
        case TEMP_SCAN:
            (…)
            break;
        case ALARM:
            if (numAlarm>0 && numAlarm<NUM_ALARM)
            {
                AudioServicesPlaySystemSound(alarma);
                AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
                NSTimeInterval delay_in_seconds = 2;
                dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW,delay_in_seconds*NSEC_PER_SEC);
                dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
                dispatch_after(delay,queue,^{
                    [self fiBGTemps:ALARM];
                });
                numAlarm++;
            }
            break;
    }
}

这种行为是否正常?我错过了什么吗?

Is that behavior normal? Am I missing something?

谢谢

Kanick

推荐答案

问题解决了。

这就是发生的事情。当应用程序在后台运行并执行任务时,此任务也在后台运行,但系统只允许一定的时间(10s围裙。)以便安全使用电池。因此它仅适用于特定任务(本地化,下载等)。

This is what happens. When an app is running in background and and it is executed a task, this task is running in background as well but the system allows only a certain amount of time (10s apron.) in order to safe battery. So it is only for a specific tasks (localization, download, etc.).

我的情况我需要更多时间,因此解决方案是将任务定义为一个UIBackgroundTask,它将成为在完成系统时通知系统并因此可以终止的任务。

I my case I need a little more time so the solution is to define the task as a UIBackgroundTask in such a way that will be the task who notify the system when it is completed and thus can be terminated.

这是它被称为任务的代码:

this is the code where it is called the task:



    UIApplication *application = [UIApplication sharedApplication];
    __block UIBackgroundTaskIdentifier background_task;
    background_task = [application beginBackgroundTaskWithExpirationHandler: ^{
        [application endBackgroundTask: background_task];
        background_task = UIBackgroundTaskInvalid;
    }];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self fiBGTemps:@(ALARM)];
        [application endBackgroundTask: background_task];
        background_task = UIBackgroundTaskInvalid;
    });

在这里你可以看到关于Apple论坛的讨论,他们在这里讨论这个问题比我。

And here you can see the discussion on the Apple Forums where they explain this better than me.

AppleForums讨论

我希望这会有所帮助。

Kanick

这篇关于dispatch_after限制为10秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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