Skip to content

Commit 15b344c

Browse files
authored
Fixes rare crash when dropping a pin (#427)
* Fixes rare crash when dropping a pin * Compile fixes * GHA tweaks, and lock GeoMonitor to 0.2.0 for TripKit 5.x
1 parent 902571c commit 15b344c

File tree

8 files changed

+41
-69
lines changed

8 files changed

+41
-69
lines changed

.github/workflows/docs-dryrun.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: macos-15
1313
steps:
1414
- name: Checkout main
15-
uses: actions/checkout@v4
15+
uses: actions/checkout@v6
1616
- uses: maxim-lobanov/setup-xcode@v1
1717
with:
1818
xcode-version: latest-stable

.github/workflows/docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
runs-on: macos-15
1313
steps:
1414
- name: Checkout main
15-
uses: actions/checkout@v4
15+
uses: actions/checkout@v6
1616
- uses: maxim-lobanov/setup-xcode@v1
1717
with:
1818
xcode-version: latest-stable

.github/workflows/swift.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
- uses: maxim-lobanov/setup-xcode@v1
3737
with:
3838
xcode-version: ${{ matrix.xcode }}
39-
- uses: actions/checkout@v4
39+
- uses: actions/checkout@v6
4040
- name: Build
4141
run: swift build --target TripKit
4242

@@ -50,7 +50,7 @@ jobs:
5050
image: swift:${{ matrix.swift }}
5151

5252
steps:
53-
- uses: actions/checkout@v4
53+
- uses: actions/checkout@v6
5454
- name: Build
5555
run: swift build --target TripKitAPI
5656

@@ -61,7 +61,7 @@ jobs:
6161
- uses: maxim-lobanov/setup-xcode@v1
6262
with:
6363
xcode-version: latest-stable
64-
- uses: actions/checkout@v3
64+
- uses: actions/checkout@v6
6565
- name: Build TripKit Mac
6666
run: xcodebuild build -quiet -project TripKit.xcodeproj -scheme "TripKit" -sdk macosx
6767
- name: Build TripKitUI iOS
@@ -76,7 +76,7 @@ jobs:
7676
- uses: maxim-lobanov/setup-xcode@v1
7777
with:
7878
xcode-version: latest-stable
79-
- uses: actions/checkout@v4
79+
- uses: actions/checkout@v6
8080
- name: Run tests
8181
env:
8282
TRIPGO_API_KEY: ${{ secrets.TRIPGO_API_KEY }}
@@ -96,7 +96,7 @@ jobs:
9696
- uses: maxim-lobanov/setup-xcode@v1
9797
with:
9898
xcode-version: latest-stable
99-
- uses: actions/checkout@v4
99+
- uses: actions/checkout@v6
100100
- name: Build TripKitUIExample
101101
run: xcodebuild build -quiet -project TripKit.xcodeproj -scheme TripKitUIExample -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 16'
102102

@@ -124,7 +124,7 @@ jobs:
124124
- uses: maxim-lobanov/setup-xcode@v1
125125
with:
126126
xcode-version: latest-stable
127-
- uses: actions/checkout@v4
127+
- uses: actions/checkout@v6
128128
- name: Build ${{ matrix.name }}
129129
run: |
130130
cd ${{ matrix.directory }}

Package.resolved

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ let package = Package(
2222
dependencies: [
2323
.package(url: "https://github.com/ReactiveX/RxSwift.git", .upToNextMajor(from: "6.1.0")),
2424
.package(url: "https://github.com/onevcat/Kingfisher.git", .upToNextMajor(from: "7.0.0")),
25-
.package(url: "https://github.com/skedgo/GeoMonitor.git", .upToNextMinor(from: "0.2.0")),
25+
.package(url: "https://github.com/skedgo/GeoMonitor.git", exact: "0.2.0"),
2626
.package(url: "https://github.com/skedgo/TGCardViewController.git", .upToNextMajor(from: "2.2.10")),
2727
],
2828
targets: [

Sources/TripKit/model/TKNamedCoordinate.swift

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,24 @@ open class TKNamedCoordinate : NSObject, NSSecureCoding, Codable, TKClusterable
4848
@objc public var placemark: CLPlacemark? {
4949
if let placemark = _placemark { return placemark }
5050
guard coordinate.isValid, TKNamedCoordinate.enableReverseGeocodingAddress, reverseGeocodingTask == nil else { return nil }
51-
52-
let coordinate = self.coordinate
53-
reverseGeocodingTask = Task { [weak self] in
54-
let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
55-
let geocoder = CLGeocoder()
56-
if let best = try? await geocoder.reverseGeocodeLocation(location).first {
57-
self?.assignPlacemark(best, includeName: false)
58-
}
51+
52+
reverseGeocodingTask = Task { @MainActor [weak self] in
53+
try? await self?.needsAddress(includeName: false)
5954
}
6055
return nil
6156
}
62-
57+
6358
private var reverseGeocodingTask: Task<Void, Never>?
59+
60+
@MainActor
61+
public func needsAddress(includeName: Bool) async throws {
62+
guard _address == nil else { return }
63+
let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
64+
let geocoder = CLGeocoder()
65+
if let best = try await geocoder.reverseGeocodeLocation(location).first {
66+
assignPlacemark(best, includeName: includeName)
67+
}
68+
}
6469

6570
@objc public var locationID: String? = nil
6671
@objc public var timeZoneID: String? = nil
@@ -129,7 +134,7 @@ open class TKNamedCoordinate : NSObject, NSSecureCoding, Codable, TKClusterable
129134
self.init(latitude: from.latitude, longitude: from.longitude, name: from.name, address: from.address)
130135
}
131136

132-
public func assignPlacemark(_ placemark: CLPlacemark, includeName: Bool) {
137+
func assignPlacemark(_ placemark: CLPlacemark, includeName: Bool) {
133138
if includeName {
134139
if let name = placemark.name {
135140
self.name = name

Sources/TripKitUI/view model/TKUIRoutingResultsViewModel+CalculateRoutes.swift

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,14 @@ extension TKUIRoutingResultsViewModel {
174174
origin = asObject.rx.observeWeakly(CLLocationCoordinate2D.self, "coordinate")
175175
.compactMap { [weak asObject] _ in asObject?.coordinate }
176176
.distinctUntilChanged(isCloseEnough)
177-
.flatMapLatest { [weak asObject] _ in RouteBuilder.needAddress(asObject, retryLimit: 3, delay: 1) }
177+
.flatMapLatest { [weak asObject] _ in RouteBuilder.needAddress(asObject) }
178178
.map { _ in (builder, Self.buildId(for: builder)) }
179179
}
180180
if let asObject = builder.destination {
181181
destination = asObject.rx.observeWeakly(CLLocationCoordinate2D.self, "coordinate")
182182
.compactMap { [weak asObject] _ in asObject?.coordinate }
183183
.distinctUntilChanged(isCloseEnough)
184-
.flatMapLatest { [weak asObject] _ in RouteBuilder.needAddress(asObject, retryLimit: 3, delay: 1) }
184+
.flatMapLatest { [weak asObject] _ in RouteBuilder.needAddress(asObject) }
185185
.map { _ in (builder, Self.buildId(for: builder)) }
186186
}
187187
return Observable.merge(origin, destination)
@@ -338,35 +338,28 @@ extension TKUIRoutingResultsViewModel.RouteBuilder {
338338
}
339339

340340
func reverseGeocodeLocations() -> Observable<(origin: String?, destination: String?)> {
341-
let originObservable = Self.needAddress(origin, retryLimit: 5, delay: 5)
342-
let destinationObservable = Self.needAddress(destination, retryLimit: 5, delay: 5)
341+
let originObservable = Self.needAddress(origin)
342+
let destinationObservable = Self.needAddress(destination)
343343
return Observable
344344
.combineLatest(originObservable, destinationObservable) { (origin: $0, destination: $1) }
345345
.distinctUntilChanged { $0.origin == $1.origin && $0.destination == $1.destination }
346346
}
347347

348-
static func needAddress(_ location: TKNamedCoordinate?, retryLimit: Int, delay: Int) -> Observable<String?> {
348+
static func needAddress(_ location: TKNamedCoordinate?) -> Observable<String?> {
349349
if let from = location?.title, from != Loc.Location {
350350
return .just(from)
351351
} else if let location {
352-
return Self.geocode(location, retryLimit: retryLimit, delay: delay).catchAndReturn(nil)
352+
return Single.create {
353+
try await location.needsAddress(includeName: true)
354+
return location.title
355+
}
356+
.asObservable()
357+
.catchAndReturn(nil)
353358
} else {
354359
return .just(nil)
355360
}
356361
}
357362

358-
private static func geocode(_ location: TKNamedCoordinate, retryLimit: Int, delay: Int) -> Observable<String?> {
359-
return CLGeocoder().rx
360-
.reverseGeocode(namedCoordinate: location)
361-
.asObservable()
362-
.retry { errors in
363-
return errors.enumerated().flatMap { (index, error) -> Observable<Int> in
364-
guard index < retryLimit else { throw error }
365-
return Observable<Int>.timer(RxTimeInterval.seconds(delay), scheduler: MainScheduler.instance)
366-
}
367-
}
368-
}
369-
370363
func generateRequest() -> TripRequest? {
371364
guard let destination = destination, let time = time else { return nil }
372365

@@ -446,30 +439,4 @@ extension TKUIRoutingResultsViewModel.RouteBuilder: Equatable {
446439
}
447440
}
448441

449-
// MARK: -
450-
451-
extension Reactive where Base: CLGeocoder {
452-
453-
func reverseGeocode(namedCoordinate: TKNamedCoordinate) -> Single<String?> {
454-
return Single.create { single in
455-
let location = CLLocation(latitude: namedCoordinate.coordinate.latitude, longitude: namedCoordinate.coordinate.longitude)
456-
457-
let geocoder = CLGeocoder()
458-
geocoder.reverseGeocodeLocation(location) { (placemarks, error) in
459-
if let error {
460-
single(.failure(error))
461-
} else {
462-
if let first = placemarks?.first {
463-
// TODO: Shouldn't always overwrite the name, e.g., if it's from a favourite
464-
namedCoordinate.assignPlacemark(first, includeName: true)
465-
}
466-
single(.success(placemarks?.first?.name))
467-
}
468-
}
469-
470-
return Disposables.create()
471-
}
472-
}
473-
474-
}
475442

TripKit.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,8 +1512,8 @@
15121512
isa = XCRemoteSwiftPackageReference;
15131513
repositoryURL = "https://github.com/skedgo/GeoMonitor";
15141514
requirement = {
1515-
kind = upToNextMajorVersion;
1516-
minimumVersion = 0.2.0;
1515+
kind = exactVersion;
1516+
version = 0.2.0;
15171517
};
15181518
};
15191519
/* End XCRemoteSwiftPackageReference section */

0 commit comments

Comments
 (0)