ios – 为什么backgroundTimeRemaining给出DBL_MAX,即使应用程序状态是UIApplicationStateBackground?

ios – 为什么backgroundTimeRemaining给出DBL_MAX,即使应用程序状态是UIApplicationStateBackground?,第1张

概述每次我的应用程序从挂起模式唤醒时,我正在记录(写入文件) backgroundTimeRemaining值,就在我开始使用到期处理程序的UIApplication后台任务之前,就像这样(在我的方法中发出网络请求): if([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground){ 每次我的应用程序从挂起模式唤醒时,我正在记录(写入文件) backgroundTimeRemaining值,就在我开始使用到期处理程序的UIApplication后台任务之前,就像这样(在我的方法中发出网络请求):

if([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground){        [Logger logIntofilenamed:@"log" withContent:[Nsstring stringWithFormat:@" state %ld,background time remaining %.2f",(long)[[UIApplication sharedApplication] applicationState],[[UIApplication sharedApplication] backgroundTimeRemaining ]] andPrettyTime:true];        UIApplication*    app = [UIApplication sharedApplication];        __block uibackgroundtaskIDentifIEr task = [app beginBackgroundTaskWithExpirationHandler:^{            [app endBackgroundTask:task];            task = uibackgroundtaskInvalID;            [Logger logIntofilenamed:@"log" withContent:@" expiration handler executed " andPrettyTime:true];        }];    }

根据文档,如果app在前台,backgroundTimeRemaining的值可能很大:

While the app is running in the foreground,the value in this property
remains suitably large.

但这不是一个例子.我的方法被执行,因为应用程序状态等于UIApplicationStateBackground.
我在这里错过了什么?

解决方法 是的,根据文件:

A value of backgroundTimeRemaining can be big if app is in the
foreground.

在提供正确的backgroundTimeRemaining之前,applicationDIDEnterBackground需要在状态之间进行一些时间.这应该在~180左右开始.使用dispatch_async在异步线程中运行它并过滤掉Foreground值(> 180).

- (voID)applicationDIDEnterBackground:(UIApplication *)application{    if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])    {        NSLog(@"Multitasking Supported");        __block uibackgroundtaskIDentifIEr background_task;        background_task = [application beginBackgroundTaskWithExpirationHandler:^ {            //Clean up code. Tell the system that we are done.            [application endBackgroundTask: background_task];            background_task = uibackgroundtaskInvalID;        }];        //To make the code block asynchronous        dispatch_async(dispatch_get_main_queue(),^{            //### background task starts            NSLog(@"Running in the background\n");            while(TRUE)            {                //#### Filter Foreground Values                if ([[UIApplication sharedApplication] backgroundTimeRemaining] < 180) {                    NSLog(@"Background time Remaining: %f",[[UIApplication sharedApplication] backgroundTimeRemaining]);                    [NSThread sleepForTimeInterval:1]; //wait for 1 sec                }            }            //#### background task ends            //Clean up code. Tell the system that we are done.            [application endBackgroundTask: background_task];            background_task = uibackgroundtaskInvalID;        });    }    else    {        NSLog(@"Multitasking Not Supported");    }}
总结

以上是内存溢出为你收集整理的ios – 为什么backgroundTimeRemaining给出DBL_MAX,即使应用程序状态是UIApplicationStateBackground?全部内容,希望文章能够帮你解决ios – 为什么backgroundTimeRemaining给出DBL_MAX,即使应用程序状态是UIApplicationStateBackground?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1002320.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存