Skip to content

Commit

Permalink
Added guarantee that CLLocationManager is created on the main thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Daniel committed Jul 5, 2019
1 parent 949e121 commit 652ea87
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
5 changes: 3 additions & 2 deletions LocoKit/Base/Helpers/MiscTools.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

import Foundation

public func onMain(_ closure: @escaping () -> ()) {
public func onMain(sync: Bool = false, _ closure: @escaping () -> ()) {
if Thread.isMainThread {
closure()
} else {
DispatchQueue.main.async(execute: closure)
sync ? DispatchQueue.main.sync(execute: closure)
: DispatchQueue.main.async(execute: closure)
}
}

Expand Down
19 changes: 13 additions & 6 deletions LocoKit/Base/LocomotionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,19 @@ import LocoKitCore
untimely deaths of small cute animals in Madagascar.
*/
@objc public private(set) lazy var locationManager: CLLocationManager = {
let manager = CLLocationManager()
manager.distanceFilter = kCLDistanceFilterNone
manager.desiredAccuracy = self.maximumDesiredLocationAccuracy
manager.pausesLocationUpdatesAutomatically = false

manager.delegate = self
var manager: CLLocationManager!

// Problems may occur if CLLocationManager is not created on the main thread
onMain(sync: true) {
manager = CLLocationManager()
manager.distanceFilter = kCLDistanceFilterNone
manager.desiredAccuracy = self.maximumDesiredLocationAccuracy
manager.pausesLocationUpdatesAutomatically = false

manager.delegate = self

}

return manager
}()

Expand Down

0 comments on commit 652ea87

Please sign in to comment.