位置管理器stopMonitoringForRegion无法正常工作 [英] Location Manager stopMonitoringForRegion Not Working

查看:182
本文介绍了位置管理器stopMonitoringForRegion无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的应用当前在后台运行时使用用户的当前位置设置区域监控。当该应用再次激活时,我正试图停止对该区域的监视,但是在大多数情况下它似乎都间歇性地工作,从而导致其无法按预期运行。当应用程序后台运行时,我开始监视该区域,并在记录详细信息时运行良好:

An app I'm working on currently sets up region monitoring with the user's current location when the app is backgrounded. When the app becomes active again I am trying to stop monitoring for the region, but it seems to work intermittently with the majority of the time resulting in it failing to act as expected. When the app is backgrounded, I start monitoring for the region and it works fine when I log the details:

- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
    DDLogInfo(@"CREATED REGION: %@", region.identifier);
}

在以下日志中会得到以下结果:

Which results in the following log:


  • 创建区域:regionFor:[半径为100处的真实纬度,此处为真实lon]

应用程序唤醒时,我调用以下函数:

When the app wakes up, I call the following function:

- (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%的时间会导致以下结果:

Which results in the following log about 75% of the time:


  • 即将停止监视1个区域

  • 停止后,我们目前正在监视1个区域地区

偶尔我会获得成功:


  • 即将停止监视1个区域

  • 停止后,我们当前正在监视0个区域

我尝试了几件事都没有成功。我要创建的区域是CLCircularRegions,它继承自CLRegion,因此无论如何都可以工作,但是在for循环中,我将CLRegion更改为CLCircularRegion无效。我原本是单独使用[locationManager MonitoredRegions],它返回一个NSSet,所以我认为使用allObjects函数来获取数组可以解决此问题,但事实并非如此。

I've tried a couple of things with no success. The regions I'm creating are CLCircularRegions, which inherit from CLRegion so that should work regardless, but in the for-loop I've changed CLRegion to CLCircularRegion with no effect. I was originally using [locationManager monitoredRegions] by itself, which returns an NSSet, so I thought using the allObjects function to get the array would fix the issue, but it hasn't.

我还认为枚举时对数组进行变异可能是一个问题,但是我在SO上看到的另一篇文章说,以上内容对他们有用...

I also thought it might be an issue with mutating the array while enumerating, but the only other post I saw on SO said that the above worked for them...

我错过了什么吗?

推荐答案

如果您阅读了 monitoredRegions ,它代表所有CLLocationManager实例的所有受监视区域,因此可能由私有调度队列控制-这将解释延迟。

If you read up on monitoredRegions, it represents all monitored regions of all CLLocationManager instances, and so is probably controlled by a private dispatch queue - which would explain the delays.

我的建议是保留您自己的可变数组(或集合),使用它来跟踪受监视的区域和不监视的区域,并且不要依赖

My suggestion would be to keep your own mutable array (or set) around, using it to keep track of what regions are monitored and which are not, and not rely on the location manager for that collection.

现在,很明显,您不能依靠它的即时更改,我会围绕它进行设计,而不是尝试寻找一些似乎启发式的方法(今天)上班,但稍后会咬你。

Now that its clear you cannot rely on immediate changes to it, I'd design around it rather than try to find some heuristic that seems (today) to work but bites you later.

这篇关于位置管理器stopMonitoringForRegion无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆