Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 后台任务占用100%CPU并耗尽电池电量_Ios_Objective C_Xcode_Background - Fatal编程技术网

Ios 后台任务占用100%CPU并耗尽电池电量

Ios 后台任务占用100%CPU并耗尽电池电量,ios,objective-c,xcode,background,Ios,Objective C,Xcode,Background,我正在开发一个企业应用程序,它必须定时一致地报告信息,尤其是当应用程序位于后台时 - (void)applicationWillResignActive:(UIApplication *)application { //stuff UIApplication *app = [UIApplication sharedApplication]; //create new uiBackgroundTask bgTask = [app beginBackgrou

我正在开发一个企业应用程序,它必须定时一致地报告信息,尤其是当应用程序位于后台时

- (void)applicationWillResignActive:(UIApplication *)application {
     //stuff
     UIApplication *app = [UIApplication sharedApplication];

     //create new uiBackgroundTask
     bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
     [app endBackgroundTask:bgTask];
     bgTask = UIBackgroundTaskInvalid;
     }];
     //and create new timer with async call:
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
     //run function methodRunAfterBackground
     timerOne = [NSTimer scheduledTimerWithTimeInterval:100 target:self selector:@selector(refreshInactiveTimer) userInfo:nil repeats:YES];
     timerTwo = [NSTimer scheduledTimerWithTimeInterval:300 target:self selector:@selector(postActivity) userInfo:nil repeats:YES];
     [[NSRunLoop currentRunLoop] addTimer:timerOne forMode:NSDefaultRunLoopMode];
     [[NSRunLoop currentRunLoop] addTimer:timerTwo forMode:NSDefaultRunLoopMode];
     [[NSRunLoop currentRunLoop] run];
     });
}

- (void)applicationWillEnterForeground:(UIApplication *)applicationq {
    [[UIApplication sharedApplication] endBackgroundTask:bgTask];
    [timerOne invalidate];
    [timerTwo invalidate];
}

-(void)refreshInactiveTimer {  //this function plays a short inaudible sound which refreshes the background timer
    AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
    AVSpeechUtterance *utter = [[AVSpeechUtterance alloc] initWithString:@"up"];
    [utter setVolume:0.0];
    [synth speakUtterance:utter];
}

-(void)postActivity {
    //this function makes a NSURLRequest that posts small data back to server side
}
代码按预期工作,但问题是根据分析器,它占用了100%的CPU。我确实注意到,当我拔掉电源插头进行测试时,电池似乎也会很快耗尽,因此CPU肯定会嘎嘎作响。也许我错过了一些小东西


注:是的,我知道这是一种不好的做法,苹果会拒绝使用这种方法的应用程序。然而,正如我所提到的,这是一个企业应用程序,它永远不会出现在应用程序商店中,也永远不会分发给公众。

我对此不是100%确定,但我认为当应用程序在后台时,计时器不会像预期的那样工作;我很确定他们会立即开火。我可以根据服务器日志和调试日志确认他们会在预期的时间间隔内无限期地开火。这是有效的,因为您在后台运行时错误地使用了播放音频模式。请注意,苹果可能不允许这种背景模式的滥用就是这样一种方式。它会耗尽电池电量,因为这是你想要它做的。您可能想看看其他解决方案,大多数情况下,应用程序不需要在后台继续运行。企业应用程序还可以使用推送通知,并且这些通知可以在iOS 7之后保持沉默,以允许您更新内容。我完全理解代码的作用和预期作用,正如我提到的,这是针对企业内部应用程序的。我已尝试使用后台获取应用程序performFetchWithCompletionHandler:void^UIBackgroundFetchResultcompletionHandler,间隔为UIApplicationBackgroundFetchIntervalMinimum;但通过这样做,信息的发布完全是零星的,而且频率不够。这很奇怪,因为在这个例子中,计时器每100秒执行一次,发出一个简短的单词。我无法理解为什么即使不执行这两个事件中的任何一个,CPU也会持续运行到90-100%。