Skip to content

Commit 5e21ad1

Browse files
author
Tom Daniel
committed
Added guarantee that CLLocationManager is created on the main thread.
1 parent 582b928 commit 5e21ad1

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

LocoKit/Base/Helpers/MiscTools.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
import Foundation
1010

11-
public func onMain(_ closure: @escaping () -> ()) {
11+
public func onMain(sync: Bool = false, _ closure: @escaping () -> ()) {
1212
if Thread.isMainThread {
1313
closure()
1414
} else {
15-
DispatchQueue.main.async(execute: closure)
15+
sync ? DispatchQueue.main.sync(execute: closure)
16+
: DispatchQueue.main.async(execute: closure)
1617
}
1718
}
1819

LocoKit/Base/LocomotionManager.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,19 @@ import LocoKitCore
467467
untimely deaths of small cute animals in Madagascar.
468468
*/
469469
@objc public private(set) lazy var locationManager: CLLocationManager = {
470-
let manager = CLLocationManager()
471-
manager.distanceFilter = kCLDistanceFilterNone
472-
manager.desiredAccuracy = self.maximumDesiredLocationAccuracy
473-
manager.pausesLocationUpdatesAutomatically = false
474-
475-
manager.delegate = self
470+
var manager: CLLocationManager!
471+
472+
// Problems may occur if CLLocationManager is not created on the main thread
473+
onMain(sync: true) {
474+
manager = CLLocationManager()
475+
manager.distanceFilter = kCLDistanceFilterNone
476+
manager.desiredAccuracy = self.maximumDesiredLocationAccuracy
477+
manager.pausesLocationUpdatesAutomatically = false
478+
479+
manager.delegate = self
480+
481+
}
482+
476483
return manager
477484
}()
478485

0 commit comments

Comments
 (0)