Apply scheduled updates to a downloaded preplanned map area.
Use case
With scheduled updates, the author can update the features within the preplanned areas on the service once, and multiple end-users can request these updates to bring their local copies up to date with the most recent state. Importantly, any number of end-users can download the same set of cached updates which means that this workflow is extremely scalable for large operations where you need to minimize load on the server.
This workflow can be used by survey workers operating in remote areas where network connectivity is not available. The workers could download mobile map packages to their individual devices and perform their work normally. Once they regain Internet connectivity, the mobile map packages can be updated to show any new features that have been added to the online service.
How to use the sample
When the sample loads, it will display an offline map, check for available updates, and show update availability and size. Tap "Apply" to apply the updates to the local offline map and show the results.
How it works
- Create an
AGSOfflineMapSyncTask
object with your offline map. - If desired, get an
AGSOfflineMapUpdatesInfo
instance from the task to check for update availability or update size. - Get a set of default parameters (
AGSOfflineMapSyncParameters
) for the task. - Set the parameters to download all available updates.
- Use the parameters to create an
AGSOfflineMapSyncJob
object. - Start the job and get the results once it completes successfully.
- Check if the mobile map package needs to be reopened, and do so if necessary.
- Finally, display your offline map to see the changes.
Relevant API
- AGSMobileMapPackage
- AGSOfflineMapSyncJob
- AGSOfflineMapSyncParameters
- AGSOfflineMapSyncResult
- AGSOfflineMapSyncTask
- AGSOfflineMapUpdatesInfo
About the data
The data in this sample shows the roads and trails in the Canyonlands National Park, Utah. Data by U.S. National Parks Service. No claim to original U.S. Government works.
Additional information
Note: preplanned areas using the Scheduled Updates workflow are read-only. For preplanned areas that can be edited on the end-user device, see the Download preplanned map area sample. For more information about offline workflows, see Offline maps, scenes, and data in the ArcGIS Developers guide.
Tags
offline, pre-planned, preplanned, synchronize, update
Sample Code
//
// Copyright © 2019 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import UIKit
import ArcGIS
/// A view controller that manages the interface of the Apply Scheduled Updates
/// to Preplanned Map Area sample.
class ApplyScheduledUpdatesToPreplannedMapAreaViewController: UIViewController {
/// The map view managed by the view controller.
@IBOutlet weak var mapView: AGSMapView!
/// The mobile map package used by this sample.
var mobileMapPackage: AGSMobileMapPackage!
/// The sync task used to check for scheduled updates.
var offlineMapSyncTask: AGSOfflineMapSyncTask!
/// The sync job used to apply updates to the offline map.
var offlineMapSyncJob: AGSOfflineMapSyncJob!
/// The temporary URL to store the mobile map package.
var temporaryMobileMapPackageURL: URL!
required init?(coder: NSCoder) {
let mobileMapPackageURL = Bundle.main.url(forResource: "canyonlands", withExtension: nil)!
do {
let temporaryDirectoryURL = try FileManager.default.url(
for: .itemReplacementDirectory,
in: .userDomainMask,
appropriateFor: mobileMapPackageURL,
create: true
)
temporaryMobileMapPackageURL = temporaryDirectoryURL.appendingPathComponent(ProcessInfo.processInfo.globallyUniqueString)
try FileManager.default.copyItem(at: mobileMapPackageURL, to: temporaryMobileMapPackageURL)
mobileMapPackage = AGSMobileMapPackage(fileURL: temporaryMobileMapPackageURL)
} catch {
print("Error setting up mobile map package: \(error)")
mobileMapPackage = nil
}
super.init(coder: coder)
mobileMapPackage?.load { [weak self] (error) in
let result: Result<Void, Error>
if let error = error {
result = .failure(error)
} else {
result = .success(())
}
self?.mobileMapPackageDidLoad(with: result)
}
}
deinit {
if let mobileMapPackage = mobileMapPackage {
mobileMapPackage.close()
try? FileManager.default.removeItem(at: mobileMapPackage.fileURL)
}
}
/// Called in response to the mobile map package load operation completing.
/// - Parameter result: The result of the load operation.
func mobileMapPackageDidLoad(with result: Result<Void, Error>) {
switch result {
case .success:
let map = self.mobileMapPackage.maps.first!
loadViewIfNeeded()
mapView.map = map
let offlineMapSyncTask = AGSOfflineMapSyncTask(map: map)
offlineMapSyncTask.checkForUpdates { [weak self] (updatesInfo, error) in
if let updatesInfo = updatesInfo {
self?.offlineMapSyncTaskDidComplete(with: .success(updatesInfo))
} else if let error = error {
self?.offlineMapSyncTaskDidComplete(with: .failure(error))
}
}
self.offlineMapSyncTask = offlineMapSyncTask
case .failure(let error):
presentAlert(error: error)
}
}
/// Called in response to the offline map sync task completing.
/// - Parameter result: The result of the sync task.
func offlineMapSyncTaskDidComplete(with result: Result<AGSOfflineMapUpdatesInfo, Error>) {
switch result {
case .success(let updatesInfo):
let alertController: UIAlertController
if updatesInfo.downloadAvailability == .available {
let downloadSize = updatesInfo.scheduledUpdatesDownloadSize
let measurement = Measurement(
value: Double(downloadSize),
unit: UnitInformationStorage.bytes
)
let downloadSizeString = ByteCountFormatter.string(from: measurement, countStyle: .file)
alertController = UIAlertController(
title: "Scheduled Updates Available",
message: "A \(downloadSizeString) update is available. Would you like to apply it?",
preferredStyle: .alert
)
let applyAction = UIAlertAction(title: "Apply", style: .default) { (_) in
self.applyScheduledUpdates()
}
alertController.addAction(applyAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
alertController.addAction(cancelAction)
alertController.preferredAction = applyAction
} else {
alertController = UIAlertController(
title: "Scheduled Updates Unavailable",
message: "There are no updates available.",
preferredStyle: .alert
)
let okayAction = UIAlertAction(title: "OK", style: .default)
alertController.addAction(okayAction)
alertController.preferredAction = okayAction
}
present(alertController, animated: true)
case .failure(let error):
presentAlert(error: error)
}
}
/// Apply available updates to the offline map.
func applyScheduledUpdates() {
offlineMapSyncTask.defaultOfflineMapSyncParameters { [weak self] (parameters, error) in
guard let self = self else { return }
if let parameters = parameters {
let offlineMapSyncJob = self.offlineMapSyncTask.offlineMapSyncJob(with: parameters)
offlineMapSyncJob.start(statusHandler: nil) { [weak self] (result, error) in
if let result = result {
self?.offlineMapSyncJobDidComplete(with: .success(result))
} else if let error = error {
self?.offlineMapSyncJobDidComplete(with: .failure(error))
}
}
self.offlineMapSyncJob = offlineMapSyncJob
} else if let error = error {
self.presentAlert(error: error)
}
}
}
/// Called in response to the offline map sync job completing.
/// - Parameter result: The result of the sync job.
func offlineMapSyncJobDidComplete(with result: Result<AGSOfflineMapSyncResult, Error>) {
switch result {
case .success(let result):
guard result.isMobileMapPackageReopenRequired else {
break
}
mobileMapPackage.close()
// Create a new instance of the updated mobile map package and load.
let updatedMobileMapPackage = AGSMobileMapPackage(fileURL: temporaryMobileMapPackageURL)
updatedMobileMapPackage.load { [weak self] (error) in
guard let self = self else { return }
if let error = error {
self.presentAlert(error: error)
} else {
self.mapView.map = self.mobileMapPackage.maps.first
}
}
mobileMapPackage = updatedMobileMapPackage
case .failure(let error):
presentAlert(error: error)
}
}
// MARK: UIViewController
override func viewDidLoad() {
super.viewDidLoad()
// add the source code button item to the right of navigation bar
(self.navigationItem.rightBarButtonItem as? SourceCodeBarButtonItem)?.filenames = [
"ApplyScheduledUpdatesToPreplannedMapAreaViewController"
]
}
}