From efed1725680a59ae03367e3f3a6b494e4ae20924 Mon Sep 17 00:00:00 2001 From: Aaron Brethorst Date: Sat, 13 Jul 2024 20:45:08 -0700 Subject: [PATCH 1/2] Debounce search queries in LocationServices This will reduce the chances of us getting throttled by MKLocalSearchCompleter's backend service --- OTPKit/Services/LocationServices.swift | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/OTPKit/Services/LocationServices.swift b/OTPKit/Services/LocationServices.swift index 11e0b52..0c74076 100644 --- a/OTPKit/Services/LocationServices.swift +++ b/OTPKit/Services/LocationServices.swift @@ -14,18 +14,31 @@ public final class LocationService: NSObject, ObservableObject, MKLocalSearchCom @Published var completions = [Location]() - init(completer: MKLocalSearchCompleter = MKLocalSearchCompleter()) { + init(completer: MKLocalSearchCompleter = MKLocalSearchCompleter(), debounceInterval: TimeInterval = 0.3) { self.completer = completer + self.debounceInterval = debounceInterval super.init() self.completer.delegate = self } - /// update method responsible for updating the queryFragement of `completer` - /// - Parameter queryFragment: this manage the searched query for `completer` + deinit { + debounceTimer?.invalidate() + } + + private let debounceInterval: TimeInterval + private var debounceTimer: Timer? + + /// Initiates a local search for `queryFragment`. + /// This will be debounced, as set by the `debounceInterval` on the initializer. + /// - Parameter queryFragment: The search term public func update(queryFragment: String) { - completer.resultTypes = .query - completer.queryFragment = queryFragment + debounceTimer?.invalidate() + debounceTimer = Timer.scheduledTimer(withTimeInterval: debounceInterval, repeats: false) { [weak self] _ in + guard let self else { return } + completer.resultTypes = .query + completer.queryFragment = queryFragment + } } /// completerDidUpdateResults is method that finished the search functionality and update the `completer`. From 621532b09c0ddec47783f631b05081af80c8e92f Mon Sep 17 00:00:00 2001 From: Aaron Brethorst Date: Sat, 13 Jul 2024 20:45:36 -0700 Subject: [PATCH 2/2] Add a prompt in the demo app for accessing the user's location --- project.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/project.yml b/project.yml index dd73278..671ed92 100644 --- a/project.yml +++ b/project.yml @@ -18,6 +18,7 @@ targets: path: OTPKitDemo/Info.plist properties: CFBundleShortVersionString: "$(MARKETING_VERSION)" + NSLocationWhenInUseUsageDescription: See where you are in relation to transit, and help you navigate more easily. type: application platform: iOS settings: