问题
When I start monitoring a region like
locationManager.startMonitoringForRegion(tGeoFence[wert][wert2])
and try to determine if it is already entered right after that like this:
for region in locationManager.monitoredRegions {
if let cireg = region as? CLCircularRegion {
if cireg.identifier == tGeoFence[wert][wert2].identifier {
locationManager.requestStateForRegion(cireg)
}
}
}
doesn't work, cause the registration of the region is not finished when the 2nd part of the code is executed. Delaying the execution of that part seems ugly (diddeterminestate not always called), is there a better way to solve this?
回答1:
Found out, obviously the didStartMonitoringForRegion delegate function is the right place to ask for requestStateForRegion:
func locationManager(manager: CLLocationManager, didStartMonitoringForRegion region: CLRegion) {
locationManager.requestStateForRegion(region)
}
Edit: Awful thing is, I still need a short delay or sometimes requestStateForRegion is not called:
func locationManager(manager: CLLocationManager, didStartMonitoringForRegion region: CLRegion) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) {
self.locationManager.requestStateForRegion(region)
}
}
: (
来源:https://stackoverflow.com/questions/33287196/requeststateforregion-after-startmonitoringforregion-in-swift