我正在处理的应用程序当前应用程序处于后台时,会根据用户的当前位置设置区域监控。当应用程序再次变为活动状态时,我正在尝试停止对该区域的监视,但它似乎在大多数时间间歇性地工作,导致它无法按预期运行。当应用程序背景化时,我开始监控该区域,并且在我记录详细信息时工作正常:
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
DDLogInfo(@"CREATED REGION: %@", region.identifier);
}
导致以下日志:
当应用程序唤醒时,我调用以下函数:
- (void)stopMonitoringAllRegions {
DDLogInfo(@"About to stop monitoring for %d regions", [locationManager monitoredRegions].count);
// stop monitoring for any and all current regions
for (CLRegion *region in [[locationManager monitoredRegions] allObjects]) {
[locationManager stopMonitoringForRegion:region];
}
DDLogInfo(@"After stopping, we're currently monitoring for %d regions", [locationManager monitoredRegions].count);
}
这导致以下日志约占75%的时间:
并且我很少得到看似成功的东西:
我尝试了一些没有成功的事情。我正在创建的区域是CLCircularRegions,它继承自CLRegion,因此无论如何都应该工作,但在for-loop中我将CLRegion更改为CLCircularRegion而没有任何效果。我最初使用[locationManager monitoredRegions]本身,它返回一个NSSet,所以我想使用allObjects函数来获取数组将解决问题,但它没有。
我也认为在枚举时改变数组可能是一个问题,但我在SO上看到的唯一一篇文章说上面的内容对他们起作用了......
我错过了什么吗?
答案 0 :(得分:7)
如果您阅读monitoredRegions
,它代表所有CLLocationManager实例的所有受监视区域,因此可能由私有调度队列控制 - 这可以解释延迟。
我的建议是保留你自己的可变数组(或集合),使用它来跟踪哪些区域被监控,哪些不是,而不依赖于该集合的位置管理器。
现在很清楚你不能立即改变它,我会围绕它设计,而不是试图找到一些似乎(今天)起作用但后来咬你的启发式。