Skip to content

Move functions accessing location into a background thread #426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 58 additions & 54 deletions ios/LocationProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,42 @@ class LocationProvider: NSObject {
}

func getCurrentLocation(_ options: LocationOptions) -> Void {
if let location = locationManager.location {
let elapsedTime = (Date().timeIntervalSince1970 - location.timestamp.timeIntervalSince1970) * 1000

#if DEBUG
NSLog("RNLocation: elapsedTime=\(elapsedTime)ms, maxAge=\(options.maximumAge)ms")
#endif

if elapsedTime < options.maximumAge {
#if DEBUG
NSLog("RNLocation: returning cached location")
#endif
delegate?.onLocationChange(self, location: location)
return
}
}
DispatchQueue.global(qos: .background).async {
if let location = self.locationManager.location {
let elapsedTime = (Date().timeIntervalSince1970 - location.timestamp.timeIntervalSince1970) * 1000

#if DEBUG
NSLog("RNLocation: elapsedTime=\(elapsedTime)ms, maxAge=\(options.maximumAge)ms")
#endif

if elapsedTime < options.maximumAge {
#if DEBUG
NSLog("RNLocation: returning cached location")
#endif
self.delegate?.onLocationChange(self, location: location)
return
}
}

isSingleUpdate = true
locationOptions = options
self.isSingleUpdate = true
self.locationOptions = options

locationManager.desiredAccuracy = options.accuracy
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.requestLocation()

let timeout = options.timeout

if timeout > 0 && timeout != Double.infinity {
timeoutTimer = Timer.scheduledTimer(
timeInterval: timeout / 1000.0, // timeInterval is in seconds
target: self,
selector: #selector(timerFired),
userInfo: nil,
repeats: false
)
}
self.locationManager.desiredAccuracy = options.accuracy
self.locationManager.distanceFilter = kCLDistanceFilterNone
self.locationManager.requestLocation()

let timeout = options.timeout

if timeout > 0 && timeout != Double.infinity {
self.timeoutTimer = Timer.scheduledTimer(
timeInterval: timeout / 1000.0, // timeInterval is in seconds
target: self,
selector: #selector(self.timerFired),
userInfo: nil,
repeats: false
)
}
}
}

func requestLocationUpdates(_ options: LocationOptions) -> Void {
Expand Down Expand Up @@ -137,31 +139,33 @@ extension LocationProvider: CLLocationManagerDelegate {
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
var err = LocationError.POSITION_UNAVAILABLE
var message: String? = nil

if let clErr = error as? CLError {
switch clErr.code {
case CLError.denied:
if !CLLocationManager.locationServicesEnabled() {
message = "Location service is turned off"
DispatchQueue.global(qos: .background).async {
var err = LocationError.POSITION_UNAVAILABLE
var message: String? = nil

if let clErr = error as? CLError {
switch clErr.code {
case CLError.denied:
if !CLLocationManager.locationServicesEnabled() {
message = "Location service is turned off"
} else {
err = LocationError.PERMISSION_DENIED
}
case CLError.network:
message = "Unable to retrieve location due to a network failure"
default:
break
}
} else {
err = LocationError.PERMISSION_DENIED
NSLog("RNLocation: \(error.localizedDescription)")
}
case CLError.network:
message = "Unable to retrieve location due to a network failure"
default:
break
}
} else {
NSLog("RNLocation: \(error.localizedDescription)")
}

delegate?.onLocationError(self, err: err, message: message)
self.delegate?.onLocationError(self, err: err, message: message)

if (isSingleUpdate) {
timeoutTimer?.invalidate()
locationManager.stopUpdatingLocation()
}
if (self.isSingleUpdate) {
self.timeoutTimer?.invalidate()
self.locationManager.stopUpdatingLocation()
}
}
}
}