我目前正在尝试在一段时间内在后台运行一个进程,以确保用户在我收集数据时仍然可以导航应用程序。我发现我应该为此使用 GCD。但是,我不确定如何arrayBuild
在后台队列中运行。
我如何也将它添加到后台队列?删除对 dispatch_async 的引用后,应用程序工作正常,所以我知道问题出在线程上。
- (IBAction)buttonPressed:(id)sender {
dispatch_async(backgroundQueue, ^{
manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest;
/* Removed for testing
[manager startUpdatingLocation];
*/
// TEST
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(arrayBuild)
userInfo:nil
repeats:YES];
// Initialise Array
resultsArray = [[NSMutableArray alloc]init];
// END TEST
});
}
- (void)arrayBuild {
loopCount++;
if (loopCount >= 11) {
// Invalidate Timer
[myTimer invalidate];
myTimer = nil;
// Find Average
NSNumber *avg = [resultsArray valueForKeyPath:@"@avg.self"];
// Change Text of Label to Average & Log
self.signal.text = [NSString stringWithFormat:@"%@",avg];
NSLog(@"%@",avg);
}else{
// Declare Signal Strength
float signalstrength = CTGetSignalStrength();
// Individual Result & Convert to Integer
NSString *result = [NSString stringWithFormat:@"%f", signalstrength];
NSInteger resultInt = [result integerValue];
// Add Object
[resultsArray addObject:[NSNumber numberWithFloat:resultInt]];
// Change Text of Label Each Second
self.signal.text = [NSString stringWithFormat:@"%d",loopCount];
NSLog(@"%f",signalstrength);
}
}