From 17922d80b9abe413c11307b6c3687f6ec779d01a Mon Sep 17 00:00:00 2001 From: Zsolt Kovacs Date: Sun, 4 Oct 2020 21:09:06 +0200 Subject: [PATCH] Forward Geocoder errors --- Sources/CLGeocoderEvents+Rx.swift | 71 +++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 12 deletions(-) diff --git a/Sources/CLGeocoderEvents+Rx.swift b/Sources/CLGeocoderEvents+Rx.swift index b51c6f7..caf29ab 100644 --- a/Sources/CLGeocoderEvents+Rx.swift +++ b/Sources/CLGeocoderEvents+Rx.swift @@ -12,17 +12,33 @@ import CoreLocation import RxCocoa #endif +private extension NSError { + static var geocoderFoundNoResult: NSError { + NSError(domain: CLError.errorDomain, code: CLError.geocodeFoundNoResult.rawValue, userInfo: nil) + } +} + extension Reactive where Base: CLGeocoder { /// Reactive wrapper for `CLGeocoder`.`reverseGeocodeLocation(_:completionHandler:)` /// used to search for placemark public func placemark(with location: CLLocation) -> Observable { return Observable.create { [base] observer in - base.reverseGeocodeLocation(location) { placemarks, _ in - observer.onNext(placemarks?.first) + base.reverseGeocodeLocation(location) { placemarks, error in + if let error = error { + observer.onError(error) + return + } + + guard let placemark = placemarks?.first else { + observer.onError(NSError.geocoderFoundNoResult) + return + } + + observer.onNext(placemark) + observer.onCompleted() } return Disposables.create { base.cancelGeocode() - observer.onCompleted() } }.unwrap() } @@ -32,12 +48,22 @@ extension Reactive where Base: CLGeocoder { @available(iOS 11.0, OSX 10.13, watchOSApplicationExtension 4.0, tvOS 11.0, *) public func placemark(with location: CLLocation, preferredLocale: Locale) -> Observable { return Observable.create { [base] observer in - base.reverseGeocodeLocation(location, preferredLocale: preferredLocale) { placemarks, _ in - observer.onNext(placemarks?.first) + base.reverseGeocodeLocation(location, preferredLocale: preferredLocale) { placemarks, error in + if let error = error { + observer.onError(error) + return + } + + guard let placemark = placemarks?.first else { + observer.onError(NSError.geocoderFoundNoResult) + return + } + + observer.onNext(placemark) + observer.onCompleted() } return Disposables.create { base.cancelGeocode() - observer.onCompleted() } }.unwrap() } @@ -46,9 +72,16 @@ extension Reactive where Base: CLGeocoder { /// used to search for placemark public func placemarks(with addressString: String) -> Observable<[CLPlacemark]> { return Observable.create { [base] observer in - base.geocodeAddressString(addressString) { placemarks, _ in + base.geocodeAddressString(addressString) { placemarks, error in + if let error = error { + observer.onError(error) + return + } + + // Forward placemarks Array, because `geocodeAddressString` might not resolve to a single location. + // See documentation for `CLGeocodeCompletionHandler` guard let placemarks = placemarks else { - observer.onError(NSError(domain: CLError.errorDomain, code: CLError.geocodeFoundNoResult.rawValue, userInfo: nil)) + observer.onError(NSError.geocoderFoundNoResult) return } @@ -65,9 +98,16 @@ extension Reactive where Base: CLGeocoder { /// used to search for placemark public func placemarks(with addressString: String, in region: CLRegion?) -> Observable<[CLPlacemark]> { return Observable.create { [base] observer in - base.geocodeAddressString(addressString, in: region) { placemarks, _ in + base.geocodeAddressString(addressString, in: region) { placemarks, error in + if let error = error { + observer.onError(error) + return + } + + // Forward placemarks Array, because `geocodeAddressString` might not resolve to a single location. + // See documentation for `CLGeocodeCompletionHandler` guard let placemarks = placemarks else { - observer.onError(NSError(domain: CLError.errorDomain, code: CLError.geocodeFoundNoResult.rawValue, userInfo: nil)) + observer.onError(NSError.geocoderFoundNoResult) return } @@ -85,9 +125,16 @@ extension Reactive where Base: CLGeocoder { @available(iOS 11.0, OSX 10.13, watchOSApplicationExtension 4.0, tvOS 11.0, *) public func geocodeAddressString(_ addressString: String, in region: CLRegion?, preferredLocale locale: Locale?) -> Observable<[CLPlacemark]> { return Observable.create { [base] observer in - base.geocodeAddressString(addressString, in: region, preferredLocale: locale) { placemarks, _ in + base.geocodeAddressString(addressString, in: region, preferredLocale: locale) { placemarks, error in + if let error = error { + observer.onError(error) + return + } + + // Forward placemarks Array, because `geocodeAddressString` might not resolve to a single location. + // See documentation for `CLGeocodeCompletionHandler` guard let placemarks = placemarks else { - observer.onError(NSError(domain: CLError.errorDomain, code: CLError.geocodeFoundNoResult.rawValue, userInfo: nil)) + observer.onError(NSError.geocoderFoundNoResult) return }