完成处理程序中的代码自我重复多次

问题描述

我为托管的 CLLocationManager 创建了一个单例并获取用户的位置。这是课程:

import Foundation

导入核心位置

typealias locationCompletionHandler = (_ isLocationAllowed: Bool,_ userCoordinate: CLLocationCoordinate2D?) -> Void

class LocationManager: NSObject {

static let shared = LocationManager()
var locationManagerCallBack: locationCompletionHandler!
var locationManager: CLLocationManager!

override init() {}

func setUpUserLocation(completion: @escaping locationCompletionHandler) {
    locationManagerCallBack = completion
    locationManager = CLLocationManager()
    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
    } else {
        locationManagerCallBack(false,nil)
    }
}

}

扩展位置管理器:CLLocationManagerDelegate { func locationManager(_ manager: CLLocationManager,didChangeAuthorization status: CLAuthorizationStatus) { 切换状态{ 案例 .denied,.restricted: locationManagerCallBack(false,nil) 案例 .authorizedAlways,.authorizedWhenInUse: locationManager.startUpdatingLocation() 案例.notDetermined: locationManager.requestAlwaysAuthorization() @unkNown 认值: 休息 } }

func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
    guard let location = locations.first?.coordinate else { return }
    locationManagerCallBack(true,location)
    locationManager.stopUpdatingLocation()
}

}

这是我实现单例时的代码

    @IBAction func transferButtonPress(_ sender: UIButton) {
    LocationManager.shared.setUpUserLocation { (isLocationEnabled,coordinate) in
        if isLocationEnabled {
            if coordinate != nil {
                print("---------- COORDINATE \(coordinate)")
                self.presenter.presentTransfers(accountModel: self.accountModel)
            } else {
                let cancelAction = UIAlertAction(title: String.Localized.cancel,style: .cancel,handler: nil)
                let openAppPreferencesAction = UIAlertAction(title: String.Localized.accept,style: .default) { _ in
                    LinkUtils.link(url: UIApplication.openSettingsURLString)
                }
                AlertController.showAlert(title: String.Localized.geolocationDeniedTitle,message: String.Localized.geolocationDeniedBody,actions: [openAppPreferencesAction,cancelAction])
            }
        } else {
            let cancelAction = UIAlertAction(title: String.Localized.cancel,handler: nil)
            let openAppPreferencesAction = UIAlertAction(title: String.Localized.accept,style: .default) { _ in
                LinkUtils.link(url: UIApplication.openSettingsURLString)
            }
            AlertController.showAlert(title: String.Localized.geolocationUnableTitle,message: String.Localized.geolocationUnableBody,cancelAction])
        }
    }
}

问题是闭包中的代码执行了几次,所以下一个屏幕加载了几次蚂蚁看起来像一个错误。我认为问题可能出在 didUpdateLocations 方法中,但我不太确定

为什么会这样?我怎样才能只执行一次该代码

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...