Skip to content

Commit

Permalink
iOS: initialize continuous location provider on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
Agontuk committed Nov 27, 2021
1 parent 01bde86 commit ff5f3be
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions ios/RNFusedLocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import CoreLocation

@objc(RNFusedLocation)
class RNFusedLocation: RCTEventEmitter {
private let locationProvider: LocationProvider
private var continuousLocationProvider: LocationProvider? = nil
private var permissionProvider: LocationProvider? = nil
private var hasListeners: Bool = false

override init() {
locationProvider = LocationProvider()
super.init()
deinit {
continuousLocationProvider = nil
permissionProvider = nil
}

// MARK: Bridge Method
Expand Down Expand Up @@ -67,7 +67,11 @@ class RNFusedLocation: RCTEventEmitter {
@objc func startLocationUpdate(_ options: [String: Any]) -> Void {
let locationOptions = LocationOptions(options)

locationProvider.requestLocationUpdates(
if continuousLocationProvider == nil {
continuousLocationProvider = LocationProvider()
}

continuousLocationProvider!.requestLocationUpdates(
locationOptions,
successHandler: { [self] (location) -> Void in
if (hasListeners) {
Expand All @@ -84,7 +88,10 @@ class RNFusedLocation: RCTEventEmitter {

// MARK: Bridge Method
@objc func stopLocationUpdate() -> Void {
locationProvider.removeLocationUpdates()
if continuousLocationProvider != nil {
continuousLocationProvider!.removeLocationUpdates()
continuousLocationProvider = nil
}
}
}

Expand Down

0 comments on commit ff5f3be

Please sign in to comment.