Skip to content

Commit cafa55c

Browse files
committed
Handle cancellation errors.
#301 (comment)
1 parent daeed27 commit cafa55c

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

Shared/Samples/Download vector tiles to local cache/DownloadVectorTilesToLocalCacheView.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ struct DownloadVectorTilesToLocalCacheView: View {
112112
isShowingResults = true
113113
// Sets downloading to false when the download finishes.
114114
isDownloading = false
115-
} catch is CancellationError {
116-
// Does nothing if the error is a cancellation error.
117115
} catch {
118116
// Shows an alert if any errors occur.
119117
self.error = error

Shared/Samples/Generate offline map/GenerateOfflineMapView.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,6 @@ private extension GenerateOfflineMapView {
216216
offlineMap = output.offlineMap
217217
// Sets the initial viewpoint of the offline map.
218218
offlineMap.initialViewpoint = Viewpoint(boundingGeometry: extent.expanded(by: 0.8))
219-
} catch is CancellationError {
220-
// Does nothing if the error is a cancellation error.
221219
} catch {
222220
// Shows an alert with the error if the job fails.
223221
self.error = error

Shared/Samples/Identify raster cell/IdentifyRasterCellView.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,6 @@ private extension IdentifyRasterCellView {
108108
// Get the first raster cell from the identify result.
109109
let rasterCell = identifyResult.geoElements.first(where: { $0 is RasterCell })
110110
return rasterCell as? RasterCell
111-
} catch is CancellationError {
112-
// Does nothing if the error is a cancellation error.
113-
return nil
114111
} catch {
115112
self.error = error
116113
return nil

Shared/Supporting Files/Extensions/View+ErrorAlert.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,19 @@ import SwiftUI
1717
extension View {
1818
/// Presents an alert with a given error's `localizedDescription` as the message.
1919
/// - Parameter error: A binding to an optional `Error` that will present the alert when
20-
/// it is not `nil`. When the user taps "OK", this value is set to `nil` and the alert is dismissed.
20+
/// it is not `nil`. When the user taps "OK", this value is set to `nil`, and the alert is dismissed.
21+
///
22+
/// If the given `error` is a `CancellationError`, it is ignored, and the alert is not presented.
2123
func errorAlert(presentingError error: Binding<Error?>) -> some View {
22-
alert("Error", isPresented: .constant(error.wrappedValue != nil), presenting: error.wrappedValue) { _ in
24+
/// A binding to a Boolean value indicating whether to present the alert.
25+
let isPresented: Binding<Bool>
26+
if error.wrappedValue != nil && !(error.wrappedValue is CancellationError) {
27+
isPresented = .constant(true)
28+
} else {
29+
isPresented = .constant(false)
30+
}
31+
32+
return alert("Error", isPresented: isPresented, presenting: error.wrappedValue) { _ in
2333
Button("OK") {
2434
error.wrappedValue = nil
2535
}

0 commit comments

Comments
 (0)