RequestStateForRegion after startMonitoringForRegion in Swift

倖福魔咒の 提交于 2019-12-12 16:40:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!