Timer on Location Manager

假装没事ソ 提交于 2019-12-25 05:21:23

问题


Is it possible to invoke didUpdateLocationsinside the Timer ? I mean..

_timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(_:didUpdateLocations:)), userInfo: nil, repeats: true) 

Is that going to work ? If yes then it is going to update location every 10 seconds ?


回答1:


You can't invoke didUpdateLocations. If you want frequent updates, your best choice is the Standard location service:

The standard location service is most appropriate for apps that deliver location-related information directly to the user but it may be used by other types of apps too. To start the service, configure the desiredAccuracy and distanceFilter properties of the location manager and then call the requestLocation(), startUpdatingLocation(), or allowDeferredLocationUpdates(untilTraveled:timeout:) method. Never specify an accuracy value greater than what you need. Core Location uses the accuracy value you specify to manage power better. A higher degree of accuracy requires more precise hardware like GPS, which consumes more power.

https://developer.apple.com/reference/corelocation/cllocationmanager




回答2:


Your callback selector has to have a different signature:

class MyClass {

    func startTimer() {
        _timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(MyClass.timerCallBack(_:)), userInfo: nil, repeats: true)
    }

    // ...

    func timerCallBack(timer: Timer) {
        // Here you go.
    }
}

(Maybe contain some syntax errors; currently don't have Xcode at hand)



来源:https://stackoverflow.com/questions/41981255/timer-on-location-manager

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