Skip to content

Instantly share code, notes, and snippets.

@nixta
Last active December 1, 2021 15:57
Show Gist options
  • Save nixta/4e4476fb286488b00b1e19b8713d01d6 to your computer and use it in GitHub Desktop.
Save nixta/4e4476fb286488b00b1e19b8713d01d6 to your computer and use it in GitHub Desktop.
Async/Await Example: Download Preplanned Offline Map
func downloadPreplannedOfflineMap() async throws {
guard let area = (try await task.getPreplannedMapAreas()).first else { return }
let params = try await task.defaultDownloadPreplannedOfflineMapParameters(with: area)
let job = task.downloadPreplannedOfflineMapJob(
with: params,
downloadDirectory: docsURL.appendingPathComponent(Date().ISO8601Format(), isDirectory: true)
)
let result = try await job.start { status in
print("Status updated")
}
offlineMap = result.offlineMap
print("Map has \(result.offlineMap.operationalLayers.count) layers and \(result.offlineMap.tables.count) tables")
}
Task {
do {
try await self.downloadPreplannedOfflineMap()
} catch {
print("Error downloading preplanned offline map: \(error.localizedDescription)")
}
}
var downloadJob: AGSDownloadPreplannedOfflineMapJob?
func downloadPreplannedOfflineMap() {
task.getPreplannedMapAreas { [weak self] areas, error in
guard let self = self else { return }
if let error = error {
print("Error getting map areas: \(error.localizedDescription)")
return
}
guard let area = areas?.first, let task = self.task else { return }
task.defaultDownloadPreplannedOfflineMapParameters(with: area) { [weak self] params, error in
guard let self = self else { return }
if let error = error {
print("Error getting preplanned map parameters: \(error.localizedDescription)")
return
}
guard let params = params else { return }
let job = task.downloadPreplannedOfflineMapJob(
with: params,
downloadDirectory: self.docsURL.appendingPathComponent(Date().ISO8601Format(), isDirectory: true))
self.downloadJob = job
job.start { status in
print("Status updated")
} completion: { result, error in
if let error = error {
print("Error downloading preplanned map: \(error.localizedDescription)")
return
}
guard let result = result else {
preconditionFailure("No error and no map result!")
}
self.offlineMap = result.offlineMap
print("Map has \(result.offlineMap.operationalLayers.count) layers and \(result.offlineMap.tables.count) tables")
}
}
}
}
@nixta
Copy link
Author

nixta commented Dec 1, 2021

This is an example of interrogating a web map to get its preplanned offline map areas and download the first one. Working with Swift Concurrency makes the code so much more palatable (see arcgis-runtime-ios-async-await).

See also https://developers.arcgis.com/ios/offline-maps-scenes-and-data/download-an-offline-map-preplanned/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment