You can take certain utility network
The following code takes a web map offline that contains a utility network. The utility network tables are automatically synced with a map when a utility network is detected within.
func initializeOfflineMapTask() async {
do {
try await map.load()
// Creates the offline map task with the web map.
offlineMapTask = OfflineMapTask(onlineMap: map)
await makeGenerateOfflineMapJob()
} catch {
print("Error loading map.")
}
}
func makeGenerateOfflineMapJob() async {
let areaOfInterest = Envelope(xMin: -13049000, yMin: 3861000, xMax: -13048000, yMax: 3861500, spatialReference: map.spatialReference)
// Creates the default parameters for the offline map task.
guard let parameters = await makeGenerateParameters(areaOfInterest: areaOfInterest) else { return }
// Creates the generate offline map job based on the parameters.
guard let offlineMapTask = offlineMapTask else { return }
generateOfflineMapJob = offlineMapTask.makeGenerateOfflineMapJob(
parameters: parameters,
downloadDirectory: makeTemporaryDirectory()
)
// Starts the offline map job.
generateOfflineMapJob.start()
do {
// Awaits the output of the job.
let output = try await generateOfflineMapJob.output
// Gets the first utility network in the offline map.
let utilityNetwork = output.offlineMap.utilityNetworks.first
try await utilityNetwork?.load()
} catch {
print("Error getting first utility network.")
}
}
func makeGenerateParameters(areaOfInterest: Envelope) async -> GenerateOfflineMapParameters? {
do {
// Retrieves the default offline map parameters for an area of interest.
let parameters = try await offlineMapTask?.makeDefaultGenerateOfflineMapParameters(areaOfInterest: areaOfInterest)
parameters?.includesBasemap = false
return parameters
} catch {
print("Error generating parameters.")
return nil
}
}
The following code takes a utility network offline using a service geodatabase. The utility network tables only get synced when the sync mode in the generate geodatabase parameters is set to sync system tables.
func initializeGeodatabaseSyncTask() async {
do {
try await map.load()
// The URL must be to a feature service hosted on ArcGIS Enterprise 10.9 or later.
let featureServiceURL = URL(string: "https://myserver.esri.com/server/rest/services/SDKTest/FeatureServer")!
geodatabaseSyncTask = GeodatabaseSyncTask(url: featureServiceURL)
await makeGenerateGeodatabaseJob()
} catch {
print("Error initializing geodatabase sync task.")
}
}
func makeGenerateGeodatabaseJob() async {
// Makes the default parameters with the area of interest.
let areaOfInterest = Envelope(xMin: 473240, yMin: 3600000, xMax: 475000, yMax: 3605000, spatialReference: .init(wkid: WKID(26911)!))
guard let parameters = await makeDefaultParameters(areaOfInterest: areaOfInterest) else { return }
parameters.utilityNetworkSyncMode = .syncSystemTables
// Creates a temporary directory to store the downloaded geodatabase.
let downloadDirectory = makeTemporaryDirectory()
generateGeodatabaseJob = geodatabaseSyncTask?.makeGenerateGeodatabaseJob(parameters: parameters, downloadFileURL: downloadDirectory)
// Starts the geodatabase.
generateGeodatabaseJob.start()
do {
// Prints the geodatabase's first utility network.
let output = try await generateGeodatabaseJob.output
guard let utilityNetwork = output.utilityNetworks.first else { return }
print(utilityNetwork.name)
} catch {
print("Error getting first utility network.")
return
}
}
// Makes the default parameters with the area of interest.
func makeDefaultParameters(areaOfInterest: Envelope) async -> GenerateGeodatabaseParameters? {
do {
return try await geodatabaseSyncTask?.makeDefaultGenerateGeodatabaseParameters(extent: areaOfInterest)
} catch {
return nil
}
}
For more information about offline workflows using ArcGIS Maps SDKs for Native Apps, see the Offline maps, scenes, and data topic.