dispatch_after is limited to 10 seconds?

前端 未结 3 2079
南方客
南方客 2021-01-23 23:04

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 m

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-23 23:40

    Problem solved.

    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.).

    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;
        });
    
    

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

    AppleForums discussion

    I hope this helps.

    Kanick

提交回复
热议问题