【问题标题】:why the didEnterRegion called twice?为什么 didEnterRegion 调用了两次?
【发布时间】:2012-12-12 19:36:46
【问题描述】:

我在我的 iOS 应用中使用位置服务,即区域监控, 这是我的代码

//this for creating region
-(void)createRegion
{
    [dictionary setValue:@"23 St, New York" forKey:@"title"];
    [dictionary setValue:[NSNumber numberWithDouble:40.742878] forKey:@"latitude"];
    [dictionary setValue:[NSNumber numberWithDouble:-73.992821] forKey:@"longitude"];
    [dictionary setValue:[NSNumber numberWithDouble:(300.0)] forKey:@"radius"];

    [regionArray addObject:[self mapDictionaryToRegion:dictionary]];
    [self initializeRegionMonitoring:regionArray];
}
- (CLRegion*)mapDictionaryToRegion:(NSDictionary*)dictionary {
    NSString *title = [dictionary valueForKey:@"title"];

    CLLocationDegrees latitude = [[dictionary valueForKey:@"latitude"] doubleValue];
    CLLocationDegrees longitude =[[dictionary valueForKey:@"longitude"] doubleValue];
    CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(latitude, longitude);

    CLLocationDistance regionRadius = [[dictionary valueForKey:@"radius"] doubleValue];

    return [[CLRegion alloc] initCircularRegionWithCenter:centerCoordinate
                                                   radius:regionRadius
                                               identifier:title];
}

- (void) initializeRegionMonitoring:(NSArray*)geofences {
    if(![CLLocationManager regionMonitoringAvailable]) {
       // [self showAlertWithMessage:@"This app requires region monitoring features which are unavailable on this device."];
        return;
    }

    for(CLRegion *geofence in geofences) {
        [_locationManager startMonitoringForRegion:geofence];
    }
}

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(@"entered region %@",region.identifier);
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    NSLog(@"exited region %@",region.identifier);
}

当应用程序处于前台时它工作正常。它向我显示日志:“进入区域..”和“退出区域..”, 但是当应用程序进入后台时,相同的日志会在几分之一秒内打印两次,即委托方法调用了两次,我不需要,有什么方法可以避免调用 2 次? 还是在创建或监视区域时犯了任何错误? 请帮我 .. 提前谢谢..

【问题讨论】:

  • 与核心问题无关但是... [dictionary setValue:[NSNumber numberWithDouble:(300.0)] forKey:@"radious"];应该是:[dictionary setValue:[NSNumber numberWithDouble:(300.0)] forKey:@"radius"];您在“半径”上有错字

标签: iphone ios cllocationmanager clregion


【解决方案1】:

我相信这个问题与我自己的问题重复。我认为 Apple 的区域监控 API 存在错误。我已经就这个问题向苹果公司提交了一份雷达报告,但到目前为止还没有收到任何回复。

我解决此问题的一种方法是将时间戳保存在 didEnter 和 didExit 方法中,如果这些方法在保存的时间戳后 10 秒内触发,则将该方法作为骗子跳过。

如果有人感兴趣,我在 github 上有一个项目展示了这个问题。

https://github.com/billburgess/GeofenceBug

请随意提交另一个雷达,因为这是 Apple 注意到问题并采取行动的唯一方式。雷达编号为 12452255 - 重复代表要求区域监控。

如果你想欺骗这个雷达,这里是开放的雷达链接,里面有信息。 http://openradar.appspot.com/radar?id=2484401

【讨论】:

  • 好的,谢谢比尔伯吉斯。我们不知道苹果什么时候会移除那个bug,但我们仍然必须手动移除那个bug,这绝对超出了我们的努力。
  • 我认为您只需要对委托方法进行速率限制。由于它们会立即同时触发,因此可以安全地将调用限制为每 5 秒或更长时间一次,具体取决于您的使用情况。在某些情况下,每分钟一次是可以接受的。祝你好运。
  • 有人在 iOS 8 上验证过这个问题吗?解决了吗?
  • 老实说,我没有尝试过测试和查看。我可能会花一些时间打开我的旧雷达测试项目,看看会发生什么。
  • 问题仍然存在。但是 didEnter 和 didExit 一个接一个地触发。因此,解决方法是按照@BillBurgess 的建议检查时间戳
猜你喜欢
  • 1970-01-01
  • 2013-11-20
  • 2019-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多