ios Swift在位置服务的getter中崩溃

ryoqjall  于 2023-03-05  发布在  iOS
关注(0)|答案(1)|浏览(331)

我收到了几个崩溃的Xcode组织者,但我不能找到它的根本原因。将是很好的,如果有人可以看看它,并给予他的想法...

...下面是getter的代码

// most recent location received
var _mostRecentLocationReceived : CLLocation = CLLocation(latitude: 0, longitude: 0)
var mostRecentLocationReceived : CLLocation {
    set (newValue) {
        _mostRecentLocationReceived = newValue
    }
    
    get {
        // check if someone already set the variable
        if ((_mostRecentLocationReceived.coordinate.latitude != 0.0)
            && (_mostRecentLocationReceived.coordinate.longitude != 0.0))
        {
            // yes, so return it
            return _mostRecentLocationReceived
            
        } else {
            
            // not set so far, provide a helper value
            
            // first atempt, the current location from location manager
            if WTS.unique.WTS_LocationManager != nil {
                if WTS.unique.WTS_LocationManager!.location != nil {
                    return WTS.unique.WTS_LocationManager!.location!
                }
            }
            
            // second atempt the last stored center of the map
            return CLLocation(latitude: WaysMapOnScreenLastCenter.latitude,
                              longitude: WaysMapOnScreenLastCenter.longitude)
        }
    }
}

Crash将此行标记为原因

if ((_mostRecentLocationReceived.coordinate.latitude != 0.0)
            && (_mostRecentLocationReceived.coordinate.longitude != 0.0))

...任何想法都欢迎!!
哈代

y4ekin9u

y4ekin9u1#

下面是最终的修复,使用HangarRash和matt的答案

// queue to handle the getter and setter of _mostRecentLocationReceivedQueue
let _mostRecentLocationReceivedQueue : DispatchQueue = DispatchQueue(
    label: "org.hobrink.WayAndSee.mostRecentLocationReceivedQueue",
    qos: .default, attributes: .concurrent)

var _mostRecentLocationReceived : CLLocation? = nil
var mostRecentLocationReceived : CLLocation {
    set (newValue) {
        _mostRecentLocationReceivedQueue.async (flags: .barrier, execute: {
            _mostRecentLocationReceived = newValue
        })
    }
    
    get {
        return _mostRecentLocationReceivedQueue.sync {
            
            // check if someone already set the variable
            if (_mostRecentLocationReceived != nil) {
                
                // yes, so return it
                return _mostRecentLocationReceived!
                
            } else {
                
                // not set so far, provide a helper value
                
                // first atempt, the current location from location manager
                if WTS.unique.WTS_LocationManager != nil {
                    if WTS.unique.WTS_LocationManager!.location != nil {
                        return WTS.unique.WTS_LocationManager!.location!
                    }
                }
                
                // second atempt the last stored center of the map
                return CLLocation(latitude: WaysMapOnScreenLastCenter.latitude,
                                  longitude: WaysMapOnScreenLastCenter.longitude)
            }
        }
    }
}

相关问题