gpt4 book ai didi

ios - 为什么当我 pop/self.dismiss 我的 WKInterfacecontroller 时不调用 deinit

转载 作者:行者123 更新时间:2023-11-28 15:12:47 38 4
gpt4 key购买 nike

我正在获取用户位置,我已经创建了单例类来在需要时获取用户位置。但它正在创建保留周期。如果我使用 LocationManager 类,则不会调用 deint。如果我不使用这个类这个 deinit 按预期正确调用。

class LocationManager: NSObject, CLLocationManagerDelegate  {
private var clLocationManager: CLLocationManager?
private var SuccessBlock:((_ latitude: String, _ longitude: String)->Void)?
private var OnFailedBlock:(()->Void)?
private var lat: String?
private var long: String?

static let shared: LocationManager = {
let instance = LocationManager()
return instance
}()

private override init() {
super.init()
}


private func invokeLocationManager() {

if self.clLocationManager == nil {

self.clLocationManager = CLLocationManager()
self.clLocationManager?.delegate = self
}

self.clLocationManager?.requestWhenInUseAuthorization()
self.clLocationManager?.startUpdatingLocation()
}


func getUserCurrentLocation(onSucessBlok onSucessBlock:@escaping (_ lat: String, _ long: String)->Void, onFailedBlok onFailedBlock:@escaping ()->Void) ->Void {

self.SuccessBlock = onSucessBlock
self.OnFailedBlock = onFailedBlock
self.invokeLocationManager()
}



func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

self.clLocationManager?.stopUpdatingLocation()
if locations.count > 0 {

let userLocation = locations[0]
self.long = String(userLocation.coordinate.longitude);
self.lat = String(userLocation.coordinate.latitude);

if self.SuccessBlock != nil {

self.SuccessBlock!(self.lat!, self.long!)
self.SuccessBlock = nil
}

} else {

if self.OnFailedBlock != nil {

self.OnFailedBlock!()
self.OnFailedBlock = nil
}
}
}

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

if self.OnFailedBlock != nil {

self.OnFailedBlock!()
self.OnFailedBlock = nil
}
}
}

From WKInterfaceController, I am calling this method

override func willActivate() {
super.willActivate()
self.getUserLocation()
}

private func getUserLocation() {

LocationManager.shared.getUserCurrentLocation(onSucessBlok: { [weak self] (lat, long) in

if CommonHelper.isStringValid(string: lat) && CommonHelper.isStringValid(string: long) {

self?.fillInfoToDictionary(value:lat, key: KLatitude, type: InfoType.Acceleration)
self?.fillInfoToDictionary(value:long, key: KLongitude, type: InfoType.Acceleration)

}

}, onFailedBlok: {


self.fillInfoToDictionary(value:"0", key: KLatitude, type: InfoType.Acceleration)
self.fillInfoToDictionary(value:"0", key: KLongitude, type: InfoType.Acceleration)
})
}

每次调用 WKInterface 的 willActivate 时,我都会调用此 getUserCurrentLocation 方法。还有其他更好的方法吗?这样就不会创建保留周期。

最佳答案

LocationManager 类本身似乎没有任何问题,但您持有对传递的 onSuccessBlockonFailedBlock 的强引用在您从 WKINterfaceController 类调用的 getUserCurrentLocation 函数中。如果您在这些 block 中保留 selfLocationManager 将强烈引用您的 WKInterfaceController 类,因此 deinit 不会被调用。你能把你的WKInterfaceController代码放上去吗?然后我们可以说更多。

编辑:是的,您在 onSuccessBlock 中将 self 捕获为 weak,但在 onFailedBlock 中捕获为强>。如果您将 [weak self] in 语句放在 onFailedBlock 的开头,它应该可以工作。

关于ios - 为什么当我 pop/self.dismiss 我的 WKInterfacecontroller 时不调用 deinit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47374463/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com