Skip to content

Generate geodatabase replica from feature service

View on GitHub

Generate a local geodatabase replica from an online feature service.

Image of Generate geodatabase replica from feature service sample

Use case

Generating a geodatabase replica is the first step toward taking a feature service offline. It allows you to save features locally for offline display.

How to use the sample

Zoom to any extent. Then tap the generate button to generate a geodatabase of features from a feature service filtered to the current extent. A red outline will show the extent used. The job's progress is shown while the geodatabase is generated. When complete, the map will reload with only the layers in the geodatabase, clipped to the extent.

How it works

  1. Create a GeodatabaseSyncTask with the URL of the feature service and load it.
  2. Create GenerateGeodatabaseParameters specifying the extent and whether to include attachments.
  3. Create a GenerateGeodatabaseJob with geodatabaseSyncTask.makeGenerateGeodatabaseJob(parameters:downloadFileURL:).
  4. Start the job and get the result Geodatabase. Inside the geodatabase are feature tables, which can be used to add feature layers to the map.
  5. Call syncTask.unregisterGeodatabase(_:) after generation when you're not planning on syncing changes to the service.

Relevant API

  • GenerateGeodatabaseJob
  • GenerateGeodatabaseParameters
  • Geodatabase
  • GeodatabaseSyncTask

Tags

disconnected, local geodatabase, offline, replica, sync

Sample Code

GenerateGeodatabaseReplicaFromFeatureServiceView.swiftGenerateGeodatabaseReplicaFromFeatureServiceView.swiftGenerateGeodatabaseReplicaFromFeatureServiceView.Model.swift
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2025 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
//
//   https://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 ArcGIS
import SwiftUI

struct GenerateGeodatabaseReplicaFromFeatureServiceView: View {
    /// The view model for the sample.
    @State private var model = Model()

    /// The text describing the status of the sample.
    @State private var statusText = ""

    /// A Boolean value indicating whether the geodatabase is being generated.
    @State private var isGeneratingGeodatabase = false

    /// The error shown in the error alert.
    @State private var error: Error?

    var body: some View {
        GeometryReader { geometryProxy in
            MapViewReader { mapViewProxy in
                MapView(map: model.map)
                    .task {
                        do {
                            try await model.setUpMap()
                            statusText = "Tap the generate button to take the area offline."
                        } catch {
                            self.error = error
                        }
                    }
                    .overlay(alignment: .top) {
                        VStack {
                            Text(statusText)
                                .multilineTextAlignment(.center)
                                .frame(maxWidth: .infinity)
                                .padding(8)
                                .background(.regularMaterial, ignoresSafeAreaEdges: .horizontal)

                            // The red rectangle representing the extent of data to include in the geodatabase.
                            Rectangle()
                                .stroke(.red, lineWidth: 2)
                                .padding(EdgeInsets(top: 20, leading: 20, bottom: 44, trailing: 20))
                                .opacity(model.geodatabase == nil ? 1 : 0)
                        }
                    }
                    .toolbar {
                        ToolbarItemGroup(placement: .bottomBar) {
                            Button("Generate Geodatabase") {
                                isGeneratingGeodatabase = true
                            }
                            .disabled(isGeneratingGeodatabase || model.geodatabase != nil)
                        }
                    }
                    .task(id: isGeneratingGeodatabase) {
                        guard isGeneratingGeodatabase else { return }
                        defer { isGeneratingGeodatabase = false }

                        do {
                            // Creates an envelope from the area of interest.
                            let viewRect = geometryProxy.frame(in: .local).inset(
                                by: UIEdgeInsets(
                                    top: 20,
                                    left: geometryProxy.safeAreaInsets.leading + 20,
                                    bottom: 44,
                                    right: -geometryProxy.safeAreaInsets.trailing + 20
                                )
                            )
                            guard let extent = mapViewProxy.envelope(
                                fromViewRect: viewRect
                            ) else { return }

                            // Generates the geodatabase using the envelope.
                            try await model.generateGeodatabase(extent: extent)
                            statusText = "Generated geodatabase successfully."
                        } catch {
                            self.error = error
                        }
                    }
                    .overlay(alignment: .center) {
                        // Shows a progress view when there is a job currently running.
                        if let progress = model.generateGeodatabaseJob?.progress {
                            VStack {
                                Text("Creating geodatabase…")
                                    .padding(.bottom)

                                ProgressView(progress)
                                    .frame(maxWidth: 180)
                            }
                            .padding()
                            .background(.ultraThickMaterial)
                            .clipShape(.rect(cornerRadius: 10))
                            .shadow(radius: 50)
                        }
                    }
                    .onDisappear {
                        Task { await model.cancelJob() }
                    }
                    .errorAlert(presentingError: $error)
            }
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.