Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何侦听来自另一个函数的委托响应?_Ios_Cllocationmanager - Fatal编程技术网

Ios 如何侦听来自另一个函数的委托响应?

Ios 如何侦听来自另一个函数的委托响应?,ios,cllocationmanager,Ios,Cllocationmanager,我想要一个名为requestData的函数,它将获取用户的当前位置,然后执行URL请求。我需要requestData函数在请求完成时有一个回调,不管它是否成功。这就是我到目前为止的想法: requestData(_ completion:@escaping ()->()){ self.locationManager.requestLocation() // Wait for the location to be updated let location:CLLoca

我想要一个名为requestData的函数,它将获取用户的当前位置,然后执行URL请求。我需要requestData函数在请求完成时有一个回调,不管它是否成功。这就是我到目前为止的想法:

requestData(_ completion:@escaping ()->()){
    self.locationManager.requestLocation()
    // Wait for the location to be updated
    let location:CLLocation? = //myLocation
    self.performRequest(with: location, completion: completion)
}
func performRequest(with location:CLLocation?, completion:@escaping ()->()){
    //Does the URL-request, and simply calls completion() when finished.
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if let location = locations.first {//Success}
    else{//Error}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) 
{//Error}
我的想法是调用requestData,它将请求myLocation,然后调用performRequest。但是CLLocationManager使用委托回调而不是块来执行
requestLocation
。我该怎么做? 如果requestLocation是这样的话,一切都会很好:

self.locationManager.requestLocation( { (locations, error) in
    if locations {}
    else {}
})
但事实并非如此


为了澄清,这是一个小部件(TodayExtension)中的代码,据我所知,它需要回调,因为我需要
widgetPerformUpdate(completionHandler:)
在触发自己的completionHandler之前等待自己的completionHandler。

CLLocation包含多个数据,以检查所获得位置的准确性和时间。您的解决方案是在请求performRequest()之前检查此数据的准确性

请查看的文档,并查看下面我的伪代码。在de
didUpdateLocations
中,您可以了解我认为适合您的解决方案。我是直接写的,所以不要讨厌这些错误

但基本上使用:

变量水平精度:CLLocationAccurance{get}

变量垂直精度:CLLocationAccuracy{get}

var时间戳:日期{get}

let位置:CLLocation//myLocation
func requestData(){
self.locationManager.requestLocation()
//等待位置更新
}
func performRequest(位置:CLLocation?,完成:@escaping()->()){
//执行URL请求,并在完成时调用completion()。
}
func locationManager(manager:CLLocationManager,didUpdateLocations位置:[CLLocation]){
如果let location=locations.first{
//当位置足够准确时,我们可以打电话
//性能要求
如果location.horizontalAccurance<15&&location.timestamp>(日期().timestampSince1970-60){
//足够准确??然后执行请求
自我执行请求(带:位置、完成:完成)
}否则{
//不够准确…请等待下一个位置更新
}
}
else{//Error}
}
func locationManager(manager:CLLocationManager,didFailWithError:error)
{//Error}

在调用performRequest()之前,您可以使用CLLocation并检查所获得位置的时间和准确性。这样,您就知道位置尽可能准确和更新。您可以将回调处理程序保存在属性中,然后从委托方法调用它
 let location:CLLocation? //myLocation

func requestData(){
    self.locationManager.requestLocation()
    // Wait for the location to be updated

}
func performRequest(with location:CLLocation?, completion:@escaping ()- >()){
//Does the URL-request, and simply calls completion() when finished.
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
    //When the location is accurate enough we can call
    //the performRequest()
  
   if location.horizontalAccuracy < 15 && location.timestamp > (Date().timestampSince1970 - 60){
        //Accurate enough?? Then do the request
         self.performRequest(with: location, completion: completion)
    
   }else{
       //Not accurate enough...wait to the next location update
   }

}
else{//Error}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) 
{//Error}