Use branch versioned data

Use branch versioned data (a type of enterprise geodatabase versioning) when you want to provide multiuser editing in a service-oriented architecture (through feature services) and your users don't need direct access to the geodatabase. To compare versioning types, see Versioning types in the ArcGIS Pro documentation.

To use branch versioning in your app:

Branched versioned data can be edited offline the same way you edit feature services offline or use individual layers offline, as described in the Edit data from a branch version section.

Why use branch versioning

With branch versioning, editors can work within their own personal version of a geodatabase so that other users don't see incomplete work and editors don't block one another from accessing the data. Each version can represent ongoing work, such as a design or a group of work orders, work that can span multiple sessions and extend over a period of weeks or months if necessary.

Some common workflows for using branch versioned datasets include:

  • Multiuser editing, such as individual field crews working in their own branches
  • Modeling a variety of outcomes, with each scenario represented in its own branch
  • Editing and tracking different phases of a project

Set up and share versioned data

If you require multiple editors concurrently accessing services with the ability to work with their own set of edits, you must first register your data as branch versioned. When you share that dataset as a service, you have the option to enable the Version Management capability at the time of publishing, which allows for the creation and management of versions. For more information about setting up a database with branch versioning, see Register a dataset as branch versioned in the ArcGIS Pro documentation.

To edit branch versioned data, you must access it through a feature service. After registering data as branch versioned, publish the data to your organization's ArcGIS Enterprise portal as a web feature layer. Be sure you publish a layer that references registered data. The data will then be available as a feature service and you can edit it as part of your branch versioning workflows. For more information about how to share branch versioned data, see ArcGIS Pro help's Share branch versioned data.

Work with branch versions

In ArcGIS Runtime, use the AGSServiceGeodatabase class to represent a hosted geodatabase. You can create an instance of the class by providing the service URI and optionally the version name. You can also read the service geodatabase property from a service feature table.

A AGSServiceFeatureTable will have an associated AGSServiceGeodatabase if the feature table was loaded from a web map or if it was created from a service geodatabase (using AGSServiceGeodatabase.tableWithLayerID()). A standalone service feature table, created using any of the constructors, will not have access to a service geodatabase.

Use AGSServiceGeodatabase.fetchVersionsWithCompletion: to get information about the versions a service geodatabase contains that the current user has access to. The information includes the name, description, access level (private, protected, or public), creation date, and so on. You can create a new version by providing a name, description, and access level. You can access public and protected versions, and private versions you own. You cannot see information about, or connect to, private versions owned by other users.

The following steps show some common tasks when working with versions in a service geodatabase. These include getting the service geodatabase used by a feature layer, getting information about the current version, creating a new version, and switching the version used by the geodatabase to the new one.

  1. Get the service feature table from the feature layer in the map.

    Use dark colors for code blocks
                                                                                                                                                 
    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
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    //
    // Copyright 2022 ESRI
    //
    // All rights reserved under the copyright laws of the United States
    // and applicable international laws, treaties, and conventions.
    //
    // You may freely redistribute and use this sample code, with or
    // without modification, provided you include the original copyright
    // notice and use restrictions.
    //
    // See the use restrictions at http://help.arcgis.com/en/sdk/10.0/usageRestrictions.htm
    //
    
    import UIKit
    import ArcGIS
    
    class ViewController: UIViewController {
        @IBOutlet var mapView: AGSMapView!
    
        var map:AGSMap!
        var geodatabase: AGSServiceGeodatabase!
        var featureTable: AGSServiceFeatureTable!
        var featureLayer: AGSFeatureLayer!
    
        var serviceGeodatabase: AGSServiceGeodatabase!
        var serviceFeatureTable: AGSServiceFeatureTable!
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.geodatabase = AGSServiceGeodatabase(url: URL(string: "https://sampleserver7.arcgisonline.com/server/rest/services/DamageAssessment/FeatureServer")!)
            // NOTE: Never hardcode login information in a production application. This is done solely for the sake of the sample.
            self.geodatabase.credential = AGSCredential(user: "editor01", password: "S7#i2LWmYH75")
            self.geodatabase.load {[weak self] error in
                guard let self = self else {return}
                if let error = error {
                    print(error.localizedDescription)
                    return
                }
                // To access the feature layer's service geodatabase the feature layer must be
                // loaded from a webmap or from a table in a service geodatabase using the
                // table(withLayerID:).
                self.featureTable = self.geodatabase.table(withLayerID: 0)
                self.featureTable.load { [weak self] error in
                    guard let self = self else {return}
                    if let error = error {
                        print(error.localizedDescription)
                        return
                    }
                    self.map = AGSMap(basemapStyle: .arcGISLightGray)
                    self.featureLayer = AGSFeatureLayer(featureTable: self.featureTable)
                    if let featureLayer = self.featureLayer {
                        self.map.operationalLayers.add(featureLayer)
                        self.mapView.map = self.map
                        self.getServiceGeodatabaseVersions()
                    }
                }
            }
        }
    
        func getServiceGeodatabaseVersions(){
    
    
            // Obtain the service feature table from the layer.
            self.serviceFeatureTable = self.featureLayer.featureTable as? AGSServiceFeatureTable
    
            // Load the service feature table.
            self.serviceFeatureTable.load {[weak self] error in
                guard let self = self else {return}
                if let error = error {
                    print(error)
                    return
                }
                // Get the service geodatabase from the service feature table.
                if let serviceGeodatabase = self.serviceFeatureTable.serviceGeodatabase {
                    self.serviceGeodatabase = serviceGeodatabase
                    self.serviceGeodatabase.load { error in
                        guard error == nil else  {
                            print("Could not load the service geodatabase \(error!.localizedDescription)")
                            return
                        }
                    }
    
                    // Check that the geodatabase supports branch versioning.
                    if self.serviceGeodatabase.supportsBranchVersioning {
    
                        // Get the service geodatabase's versions.
                        self.serviceGeodatabase.fetchVersions(completion: {[weak self] versions, error in
                            guard let self = self else {return}
                            guard error == nil else {
                                print("Could not obtain the versions \(error!.localizedDescription)")
                                return
                            }
                            if let currentVersionInfo = versions!.first(where: { $0.name < self.serviceGeodatabase.versionName }) {
                                print("The current geodatabase version is \(currentVersionInfo.name)")
                            }
    
                            self.switchToANewVersion()
                        })
                    }
                }
            }
        }
    
        func switchToANewVersion(){
    
            // Create service version parameters to define a new private version.
            // The .name value must be unique to the geodatabase, otherwise the
            // createVersion method will fail.
            let versionParams = AGSServiceVersionParameters()
            versionParams.access = .private
            versionParams.name = "ProvideAUniqueName"
            versionParams.parametersDescription = "A new design"
    
            // Create a new version of the service geodatabase.
            self.serviceGeodatabase.createVersion(with: versionParams) {[weak self] serviceVersionInfo, error in
                guard let self = self else {return}
                if let error = error {
                    print("Error \(error)")
                    return
                }
                if let serviceVersionInfo = serviceVersionInfo {
                    // Switch the service geodatabase to this new version.
                    self.serviceGeodatabase.switchVersion(withName: serviceVersionInfo.name, completion: { error in
                        if let error = error {
                            print("Error \(error.localizedDescription)")
                            return
                        }
                        print("The new geodatabase version is \(self.serviceGeodatabase.versionName)")
                    })
                }
    
            }
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    
  2. Get the service geodatabase from the service feature table. Make sure the service geodatabase is loaded. Loading the service feature table will also load its service geodatabase.

    Use dark colors for code blocks
                                                                                                                                                 
    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
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    //
    // Copyright 2022 ESRI
    //
    // All rights reserved under the copyright laws of the United States
    // and applicable international laws, treaties, and conventions.
    //
    // You may freely redistribute and use this sample code, with or
    // without modification, provided you include the original copyright
    // notice and use restrictions.
    //
    // See the use restrictions at http://help.arcgis.com/en/sdk/10.0/usageRestrictions.htm
    //
    
    import UIKit
    import ArcGIS
    
    class ViewController: UIViewController {
        @IBOutlet var mapView: AGSMapView!
    
        var map:AGSMap!
        var geodatabase: AGSServiceGeodatabase!
        var featureTable: AGSServiceFeatureTable!
        var featureLayer: AGSFeatureLayer!
    
        var serviceGeodatabase: AGSServiceGeodatabase!
        var serviceFeatureTable: AGSServiceFeatureTable!
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.geodatabase = AGSServiceGeodatabase(url: URL(string: "https://sampleserver7.arcgisonline.com/server/rest/services/DamageAssessment/FeatureServer")!)
            // NOTE: Never hardcode login information in a production application. This is done solely for the sake of the sample.
            self.geodatabase.credential = AGSCredential(user: "editor01", password: "S7#i2LWmYH75")
            self.geodatabase.load {[weak self] error in
                guard let self = self else {return}
                if let error = error {
                    print(error.localizedDescription)
                    return
                }
                // To access the feature layer's service geodatabase the feature layer must be
                // loaded from a webmap or from a table in a service geodatabase using the
                // table(withLayerID:).
                self.featureTable = self.geodatabase.table(withLayerID: 0)
                self.featureTable.load { [weak self] error in
                    guard let self = self else {return}
                    if let error = error {
                        print(error.localizedDescription)
                        return
                    }
                    self.map = AGSMap(basemapStyle: .arcGISLightGray)
                    self.featureLayer = AGSFeatureLayer(featureTable: self.featureTable)
                    if let featureLayer = self.featureLayer {
                        self.map.operationalLayers.add(featureLayer)
                        self.mapView.map = self.map
                        self.getServiceGeodatabaseVersions()
                    }
                }
            }
        }
    
        func getServiceGeodatabaseVersions(){
    
    
            // Obtain the service feature table from the layer.
            self.serviceFeatureTable = self.featureLayer.featureTable as? AGSServiceFeatureTable
    
            // Load the service feature table.
            self.serviceFeatureTable.load {[weak self] error in
                guard let self = self else {return}
                if let error = error {
                    print(error)
                    return
                }
                // Get the service geodatabase from the service feature table.
                if let serviceGeodatabase = self.serviceFeatureTable.serviceGeodatabase {
                    self.serviceGeodatabase = serviceGeodatabase
                    self.serviceGeodatabase.load { error in
                        guard error == nil else  {
                            print("Could not load the service geodatabase \(error!.localizedDescription)")
                            return
                        }
                    }
    
                    // Check that the geodatabase supports branch versioning.
                    if self.serviceGeodatabase.supportsBranchVersioning {
    
                        // Get the service geodatabase's versions.
                        self.serviceGeodatabase.fetchVersions(completion: {[weak self] versions, error in
                            guard let self = self else {return}
                            guard error == nil else {
                                print("Could not obtain the versions \(error!.localizedDescription)")
                                return
                            }
                            if let currentVersionInfo = versions!.first(where: { $0.name < self.serviceGeodatabase.versionName }) {
                                print("The current geodatabase version is \(currentVersionInfo.name)")
                            }
    
                            self.switchToANewVersion()
                        })
                    }
                }
            }
        }
    
        func switchToANewVersion(){
    
            // Create service version parameters to define a new private version.
            // The .name value must be unique to the geodatabase, otherwise the
            // createVersion method will fail.
            let versionParams = AGSServiceVersionParameters()
            versionParams.access = .private
            versionParams.name = "ProvideAUniqueName"
            versionParams.parametersDescription = "A new design"
    
            // Create a new version of the service geodatabase.
            self.serviceGeodatabase.createVersion(with: versionParams) {[weak self] serviceVersionInfo, error in
                guard let self = self else {return}
                if let error = error {
                    print("Error \(error)")
                    return
                }
                if let serviceVersionInfo = serviceVersionInfo {
                    // Switch the service geodatabase to this new version.
                    self.serviceGeodatabase.switchVersion(withName: serviceVersionInfo.name, completion: { error in
                        if let error = error {
                            print("Error \(error.localizedDescription)")
                            return
                        }
                        print("The new geodatabase version is \(self.serviceGeodatabase.versionName)")
                    })
                }
    
            }
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    
  3. See if the service geodatabase supports branch versioning. If it does, get a list of its versions and information about the current version.

    Use dark colors for code blocks
                                                                                                                                                 
    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
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    //
    // Copyright 2022 ESRI
    //
    // All rights reserved under the copyright laws of the United States
    // and applicable international laws, treaties, and conventions.
    //
    // You may freely redistribute and use this sample code, with or
    // without modification, provided you include the original copyright
    // notice and use restrictions.
    //
    // See the use restrictions at http://help.arcgis.com/en/sdk/10.0/usageRestrictions.htm
    //
    
    import UIKit
    import ArcGIS
    
    class ViewController: UIViewController {
        @IBOutlet var mapView: AGSMapView!
    
        var map:AGSMap!
        var geodatabase: AGSServiceGeodatabase!
        var featureTable: AGSServiceFeatureTable!
        var featureLayer: AGSFeatureLayer!
    
        var serviceGeodatabase: AGSServiceGeodatabase!
        var serviceFeatureTable: AGSServiceFeatureTable!
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.geodatabase = AGSServiceGeodatabase(url: URL(string: "https://sampleserver7.arcgisonline.com/server/rest/services/DamageAssessment/FeatureServer")!)
            // NOTE: Never hardcode login information in a production application. This is done solely for the sake of the sample.
            self.geodatabase.credential = AGSCredential(user: "editor01", password: "S7#i2LWmYH75")
            self.geodatabase.load {[weak self] error in
                guard let self = self else {return}
                if let error = error {
                    print(error.localizedDescription)
                    return
                }
                // To access the feature layer's service geodatabase the feature layer must be
                // loaded from a webmap or from a table in a service geodatabase using the
                // table(withLayerID:).
                self.featureTable = self.geodatabase.table(withLayerID: 0)
                self.featureTable.load { [weak self] error in
                    guard let self = self else {return}
                    if let error = error {
                        print(error.localizedDescription)
                        return
                    }
                    self.map = AGSMap(basemapStyle: .arcGISLightGray)
                    self.featureLayer = AGSFeatureLayer(featureTable: self.featureTable)
                    if let featureLayer = self.featureLayer {
                        self.map.operationalLayers.add(featureLayer)
                        self.mapView.map = self.map
                        self.getServiceGeodatabaseVersions()
                    }
                }
            }
        }
    
        func getServiceGeodatabaseVersions(){
    
    
            // Obtain the service feature table from the layer.
            self.serviceFeatureTable = self.featureLayer.featureTable as? AGSServiceFeatureTable
    
            // Load the service feature table.
            self.serviceFeatureTable.load {[weak self] error in
                guard let self = self else {return}
                if let error = error {
                    print(error)
                    return
                }
                // Get the service geodatabase from the service feature table.
                if let serviceGeodatabase = self.serviceFeatureTable.serviceGeodatabase {
                    self.serviceGeodatabase = serviceGeodatabase
                    self.serviceGeodatabase.load { error in
                        guard error == nil else  {
                            print("Could not load the service geodatabase \(error!.localizedDescription)")
                            return
                        }
                    }
    
                    // Check that the geodatabase supports branch versioning.
                    if self.serviceGeodatabase.supportsBranchVersioning {
    
                        // Get the service geodatabase's versions.
                        self.serviceGeodatabase.fetchVersions(completion: {[weak self] versions, error in
                            guard let self = self else {return}
                            guard error == nil else {
                                print("Could not obtain the versions \(error!.localizedDescription)")
                                return
                            }
                            if let currentVersionInfo = versions!.first(where: { $0.name < self.serviceGeodatabase.versionName }) {
                                print("The current geodatabase version is \(currentVersionInfo.name)")
                            }
    
                            self.switchToANewVersion()
                        })
                    }
                }
            }
        }
    
        func switchToANewVersion(){
    
            // Create service version parameters to define a new private version.
            // The .name value must be unique to the geodatabase, otherwise the
            // createVersion method will fail.
            let versionParams = AGSServiceVersionParameters()
            versionParams.access = .private
            versionParams.name = "ProvideAUniqueName"
            versionParams.parametersDescription = "A new design"
    
            // Create a new version of the service geodatabase.
            self.serviceGeodatabase.createVersion(with: versionParams) {[weak self] serviceVersionInfo, error in
                guard let self = self else {return}
                if let error = error {
                    print("Error \(error)")
                    return
                }
                if let serviceVersionInfo = serviceVersionInfo {
                    // Switch the service geodatabase to this new version.
                    self.serviceGeodatabase.switchVersion(withName: serviceVersionInfo.name, completion: { error in
                        if let error = error {
                            print("Error \(error.localizedDescription)")
                            return
                        }
                        print("The new geodatabase version is \(self.serviceGeodatabase.versionName)")
                    })
                }
    
            }
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    
  4. Create service version parameters that define properties for a new version: a name, description, and access level.

    Use dark colors for code blocks
                                                                                                                                                 
    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
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    //
    // Copyright 2022 ESRI
    //
    // All rights reserved under the copyright laws of the United States
    // and applicable international laws, treaties, and conventions.
    //
    // You may freely redistribute and use this sample code, with or
    // without modification, provided you include the original copyright
    // notice and use restrictions.
    //
    // See the use restrictions at http://help.arcgis.com/en/sdk/10.0/usageRestrictions.htm
    //
    
    import UIKit
    import ArcGIS
    
    class ViewController: UIViewController {
        @IBOutlet var mapView: AGSMapView!
    
        var map:AGSMap!
        var geodatabase: AGSServiceGeodatabase!
        var featureTable: AGSServiceFeatureTable!
        var featureLayer: AGSFeatureLayer!
    
        var serviceGeodatabase: AGSServiceGeodatabase!
        var serviceFeatureTable: AGSServiceFeatureTable!
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.geodatabase = AGSServiceGeodatabase(url: URL(string: "https://sampleserver7.arcgisonline.com/server/rest/services/DamageAssessment/FeatureServer")!)
            // NOTE: Never hardcode login information in a production application. This is done solely for the sake of the sample.
            self.geodatabase.credential = AGSCredential(user: "editor01", password: "S7#i2LWmYH75")
            self.geodatabase.load {[weak self] error in
                guard let self = self else {return}
                if let error = error {
                    print(error.localizedDescription)
                    return
                }
                // To access the feature layer's service geodatabase the feature layer must be
                // loaded from a webmap or from a table in a service geodatabase using the
                // table(withLayerID:).
                self.featureTable = self.geodatabase.table(withLayerID: 0)
                self.featureTable.load { [weak self] error in
                    guard let self = self else {return}
                    if let error = error {
                        print(error.localizedDescription)
                        return
                    }
                    self.map = AGSMap(basemapStyle: .arcGISLightGray)
                    self.featureLayer = AGSFeatureLayer(featureTable: self.featureTable)
                    if let featureLayer = self.featureLayer {
                        self.map.operationalLayers.add(featureLayer)
                        self.mapView.map = self.map
                        self.getServiceGeodatabaseVersions()
                    }
                }
            }
        }
    
        func getServiceGeodatabaseVersions(){
    
    
            // Obtain the service feature table from the layer.
            self.serviceFeatureTable = self.featureLayer.featureTable as? AGSServiceFeatureTable
    
            // Load the service feature table.
            self.serviceFeatureTable.load {[weak self] error in
                guard let self = self else {return}
                if let error = error {
                    print(error)
                    return
                }
                // Get the service geodatabase from the service feature table.
                if let serviceGeodatabase = self.serviceFeatureTable.serviceGeodatabase {
                    self.serviceGeodatabase = serviceGeodatabase
                    self.serviceGeodatabase.load { error in
                        guard error == nil else  {
                            print("Could not load the service geodatabase \(error!.localizedDescription)")
                            return
                        }
                    }
    
                    // Check that the geodatabase supports branch versioning.
                    if self.serviceGeodatabase.supportsBranchVersioning {
    
                        // Get the service geodatabase's versions.
                        self.serviceGeodatabase.fetchVersions(completion: {[weak self] versions, error in
                            guard let self = self else {return}
                            guard error == nil else {
                                print("Could not obtain the versions \(error!.localizedDescription)")
                                return
                            }
                            if let currentVersionInfo = versions!.first(where: { $0.name < self.serviceGeodatabase.versionName }) {
                                print("The current geodatabase version is \(currentVersionInfo.name)")
                            }
    
                            self.switchToANewVersion()
                        })
                    }
                }
            }
        }
    
        func switchToANewVersion(){
    
            // Create service version parameters to define a new private version.
            // The .name value must be unique to the geodatabase, otherwise the
            // createVersion method will fail.
            let versionParams = AGSServiceVersionParameters()
            versionParams.access = .private
            versionParams.name = "ProvideAUniqueName"
            versionParams.parametersDescription = "A new design"
    
            // Create a new version of the service geodatabase.
            self.serviceGeodatabase.createVersion(with: versionParams) {[weak self] serviceVersionInfo, error in
                guard let self = self else {return}
                if let error = error {
                    print("Error \(error)")
                    return
                }
                if let serviceVersionInfo = serviceVersionInfo {
                    // Switch the service geodatabase to this new version.
                    self.serviceGeodatabase.switchVersion(withName: serviceVersionInfo.name, completion: { error in
                        if let error = error {
                            print("Error \(error.localizedDescription)")
                            return
                        }
                        print("The new geodatabase version is \(self.serviceGeodatabase.versionName)")
                    })
                }
    
            }
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    
  5. Pass the parameters to the service geodatabase to create a new version. Then switch the current version of the database to the new one.

    Use dark colors for code blocks
                                                                                                                                                 
    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
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    //
    // Copyright 2022 ESRI
    //
    // All rights reserved under the copyright laws of the United States
    // and applicable international laws, treaties, and conventions.
    //
    // You may freely redistribute and use this sample code, with or
    // without modification, provided you include the original copyright
    // notice and use restrictions.
    //
    // See the use restrictions at http://help.arcgis.com/en/sdk/10.0/usageRestrictions.htm
    //
    
    import UIKit
    import ArcGIS
    
    class ViewController: UIViewController {
        @IBOutlet var mapView: AGSMapView!
    
        var map:AGSMap!
        var geodatabase: AGSServiceGeodatabase!
        var featureTable: AGSServiceFeatureTable!
        var featureLayer: AGSFeatureLayer!
    
        var serviceGeodatabase: AGSServiceGeodatabase!
        var serviceFeatureTable: AGSServiceFeatureTable!
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.geodatabase = AGSServiceGeodatabase(url: URL(string: "https://sampleserver7.arcgisonline.com/server/rest/services/DamageAssessment/FeatureServer")!)
            // NOTE: Never hardcode login information in a production application. This is done solely for the sake of the sample.
            self.geodatabase.credential = AGSCredential(user: "editor01", password: "S7#i2LWmYH75")
            self.geodatabase.load {[weak self] error in
                guard let self = self else {return}
                if let error = error {
                    print(error.localizedDescription)
                    return
                }
                // To access the feature layer's service geodatabase the feature layer must be
                // loaded from a webmap or from a table in a service geodatabase using the
                // table(withLayerID:).
                self.featureTable = self.geodatabase.table(withLayerID: 0)
                self.featureTable.load { [weak self] error in
                    guard let self = self else {return}
                    if let error = error {
                        print(error.localizedDescription)
                        return
                    }
                    self.map = AGSMap(basemapStyle: .arcGISLightGray)
                    self.featureLayer = AGSFeatureLayer(featureTable: self.featureTable)
                    if let featureLayer = self.featureLayer {
                        self.map.operationalLayers.add(featureLayer)
                        self.mapView.map = self.map
                        self.getServiceGeodatabaseVersions()
                    }
                }
            }
        }
    
        func getServiceGeodatabaseVersions(){
    
    
            // Obtain the service feature table from the layer.
            self.serviceFeatureTable = self.featureLayer.featureTable as? AGSServiceFeatureTable
    
            // Load the service feature table.
            self.serviceFeatureTable.load {[weak self] error in
                guard let self = self else {return}
                if let error = error {
                    print(error)
                    return
                }
                // Get the service geodatabase from the service feature table.
                if let serviceGeodatabase = self.serviceFeatureTable.serviceGeodatabase {
                    self.serviceGeodatabase = serviceGeodatabase
                    self.serviceGeodatabase.load { error in
                        guard error == nil else  {
                            print("Could not load the service geodatabase \(error!.localizedDescription)")
                            return
                        }
                    }
    
                    // Check that the geodatabase supports branch versioning.
                    if self.serviceGeodatabase.supportsBranchVersioning {
    
                        // Get the service geodatabase's versions.
                        self.serviceGeodatabase.fetchVersions(completion: {[weak self] versions, error in
                            guard let self = self else {return}
                            guard error == nil else {
                                print("Could not obtain the versions \(error!.localizedDescription)")
                                return
                            }
                            if let currentVersionInfo = versions!.first(where: { $0.name < self.serviceGeodatabase.versionName }) {
                                print("The current geodatabase version is \(currentVersionInfo.name)")
                            }
    
                            self.switchToANewVersion()
                        })
                    }
                }
            }
        }
    
        func switchToANewVersion(){
    
            // Create service version parameters to define a new private version.
            // The .name value must be unique to the geodatabase, otherwise the
            // createVersion method will fail.
            let versionParams = AGSServiceVersionParameters()
            versionParams.access = .private
            versionParams.name = "ProvideAUniqueName"
            versionParams.parametersDescription = "A new design"
    
            // Create a new version of the service geodatabase.
            self.serviceGeodatabase.createVersion(with: versionParams) {[weak self] serviceVersionInfo, error in
                guard let self = self else {return}
                if let error = error {
                    print("Error \(error)")
                    return
                }
                if let serviceVersionInfo = serviceVersionInfo {
                    // Switch the service geodatabase to this new version.
                    self.serviceGeodatabase.switchVersion(withName: serviceVersionInfo.name, completion: { error in
                        if let error = error {
                            print("Error \(error.localizedDescription)")
                            return
                        }
                        print("The new geodatabase version is \(self.serviceGeodatabase.versionName)")
                    })
                }
    
            }
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    

Edit data from a branch version

Edits can be made to service feature tables from a branch version in the same way they are made to any AGSServiceFeatureTable. You can use the same workflow described in the Edit topic. Within that standard workflow, the AGSServiceGeodatabase class provides some additional members for managing edits. For example, all local edits (to all tables in the version) can be applied by calling AGSServiceGeodatabase.applyEditsWithCompletion: and you can undo all edits with AGSServiceGeodatabase.undoLocalEditsWithCompletion:. Reconciling and posting versions are back-office operations that should be performed with ArcGIS Pro or other enterprise processes.

  1. Make edits to the service feature tables, such as adding, deleting, or updating features. Edits to attachments and adding or removing relates can also be managed by the service geodatabase. See Perform Edits for more information about how to perform these feature edits.

    Use dark colors for code blocks
                                                                                                                               
    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
    115
    116
    117
    118
    119
    120
    121
    122
    123
    //
    // Copyright 2022 ESRI
    //
    // All rights reserved under the copyright laws of the United States
    // and applicable international laws, treaties, and conventions.
    //
    // You may freely redistribute and use this sample code, with or
    // without modification, provided you include the original copyright
    // notice and use restrictions.
    //
    // See the use restrictions at http://help.arcgis.com/en/sdk/10.0/usageRestrictions.htm
    //
    
    import UIKit
    import ArcGIS
    
    class ViewController: UIViewController {
        @IBOutlet var mapView: AGSMapView!
    
        var map:AGSMap!
        var serviceFeatureTable: AGSServiceFeatureTable!
        var featureLayer: AGSFeatureLayer!
        var serviceGeodatabase: AGSServiceGeodatabase!
    
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // The feature layer must be loaded from a webmap or from a table in a
            // service geodatabase using the table(withLayerID:) (as shown here).
            self.serviceGeodatabase = AGSServiceGeodatabase(url: URL(string: "https://sampleserver7.arcgisonline.com/server/rest/services/DamageAssessment/FeatureServer")!)
            // NOTE: Never hardcode login information in a production application. This is done solely for the sake of this code snippet.
            self.serviceGeodatabase.credential = AGSCredential(user: "editor01", password: "S7#i2LWmYH75")
            self.serviceGeodatabase.load {[weak self] error in
                guard let self = self else {return}
                if let error = error {
                    print(error.localizedDescription)
                    return
                }
                self.serviceFeatureTable = self.serviceGeodatabase.table(withLayerID: 0)
                self.serviceFeatureTable.load { [weak self] error in
                    guard let self = self else {return}
                    if let error = error {
                        print(error.localizedDescription)
                        return
                    }
                    self.map = AGSMap(basemapStyle: .arcGISLightGray)
                    self.featureLayer = AGSFeatureLayer(featureTable: self.serviceFeatureTable)
                    if let featureLayer = self.featureLayer {
                        self.map.operationalLayers.add(featureLayer)
                        self.mapView.map = self.map
                        self.editData()
                    }
                }
            }
        }
    
        func editData(){
    
    
            // Create a new feature and define its geometry and attributes.
            let newFeature = self.serviceFeatureTable.createFeature()
            newFeature.geometry = AGSPoint(x: 1036000, y: 1860000, spatialReference:self.serviceFeatureTable.spatialReference)
            newFeature.attributes["pstlcity"] = "London"
    
            // Add the new feature to the local table.
            self.serviceFeatureTable.add(newFeature)  {(error: Error?) in
                if let error = error {
                    print("Error", error)
                }
            }
    
            // Use a query to return specific features.
            let queryParams = AGSQueryParameters()
            queryParams.whereClause = "pstlcity = 'London'"
            self.serviceFeatureTable.queryFeatures(with: queryParams) { [weak self] (result, error) in
                guard let self = self else { return }
                if let error = error {
                    print(error.localizedDescription)
                }
                if let features = result?.featureEnumerator().allObjects {
                    if !features.isEmpty {
    
                        // Delete these features.
                        self.serviceFeatureTable.delete(features) { (error: Error?) in
                            if let error = error {
                                print("Error while deleting features : \(error.localizedDescription)")
                            }
                        }
                    }
                }
            }
    
            // Obtain an array of tables managed by the service geodatabase.
            let connectedTables = self.serviceGeodatabase.connectedTables
    
            // Check if the service geodatabase has any local edits?
            if self.serviceGeodatabase.hasLocalEdits() {
    
                // Apply edits to the service for all of the tables in the service
                // geodatabase to ensure that the 'database behavior' is honored.
                self.serviceGeodatabase.applyEdits {(featureTableEditResults: [AGSFeatureTableEditResult]?, error: Error?) in
                    if let error = error {
                        print("Error while applying edits :: \(error.localizedDescription)")
                    }
                }
            }
    
            // Undo the local edits for all the tables in the service geodatabase.
            self.serviceGeodatabase.undoLocalEdits {(error) in
                if let error = error {
                    print("Error while undoing edits :: \(error.localizedDescription)")
                }
            }
    
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    
  2. Service feature table edits only affect the local datasets unless they are explicitly applied to the service. Before applying your local edits, you may want to examine the connected tables for a service geodatabase to see which tables it manages. When loading a web map, the service geodatabase's connected tables will consist of all tables in the map from the same feature service. Accessing a table from the service geodatabase (using AGSServiceGeodatabase.tableWithLayerID()) will also add that table to the collection of connected tables.

    Use dark colors for code blocks
                                                                                                                               
    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
    115
    116
    117
    118
    119
    120
    121
    122
    123
    //
    // Copyright 2022 ESRI
    //
    // All rights reserved under the copyright laws of the United States
    // and applicable international laws, treaties, and conventions.
    //
    // You may freely redistribute and use this sample code, with or
    // without modification, provided you include the original copyright
    // notice and use restrictions.
    //
    // See the use restrictions at http://help.arcgis.com/en/sdk/10.0/usageRestrictions.htm
    //
    
    import UIKit
    import ArcGIS
    
    class ViewController: UIViewController {
        @IBOutlet var mapView: AGSMapView!
    
        var map:AGSMap!
        var serviceFeatureTable: AGSServiceFeatureTable!
        var featureLayer: AGSFeatureLayer!
        var serviceGeodatabase: AGSServiceGeodatabase!
    
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // The feature layer must be loaded from a webmap or from a table in a
            // service geodatabase using the table(withLayerID:) (as shown here).
            self.serviceGeodatabase = AGSServiceGeodatabase(url: URL(string: "https://sampleserver7.arcgisonline.com/server/rest/services/DamageAssessment/FeatureServer")!)
            // NOTE: Never hardcode login information in a production application. This is done solely for the sake of this code snippet.
            self.serviceGeodatabase.credential = AGSCredential(user: "editor01", password: "S7#i2LWmYH75")
            self.serviceGeodatabase.load {[weak self] error in
                guard let self = self else {return}
                if let error = error {
                    print(error.localizedDescription)
                    return
                }
                self.serviceFeatureTable = self.serviceGeodatabase.table(withLayerID: 0)
                self.serviceFeatureTable.load { [weak self] error in
                    guard let self = self else {return}
                    if let error = error {
                        print(error.localizedDescription)
                        return
                    }
                    self.map = AGSMap(basemapStyle: .arcGISLightGray)
                    self.featureLayer = AGSFeatureLayer(featureTable: self.serviceFeatureTable)
                    if let featureLayer = self.featureLayer {
                        self.map.operationalLayers.add(featureLayer)
                        self.mapView.map = self.map
                        self.editData()
                    }
                }
            }
        }
    
        func editData(){
    
    
            // Create a new feature and define its geometry and attributes.
            let newFeature = self.serviceFeatureTable.createFeature()
            newFeature.geometry = AGSPoint(x: 1036000, y: 1860000, spatialReference:self.serviceFeatureTable.spatialReference)
            newFeature.attributes["pstlcity"] = "London"
    
            // Add the new feature to the local table.
            self.serviceFeatureTable.add(newFeature)  {(error: Error?) in
                if let error = error {
                    print("Error", error)
                }
            }
    
            // Use a query to return specific features.
            let queryParams = AGSQueryParameters()
            queryParams.whereClause = "pstlcity = 'London'"
            self.serviceFeatureTable.queryFeatures(with: queryParams) { [weak self] (result, error) in
                guard let self = self else { return }
                if let error = error {
                    print(error.localizedDescription)
                }
                if let features = result?.featureEnumerator().allObjects {
                    if !features.isEmpty {
    
                        // Delete these features.
                        self.serviceFeatureTable.delete(features) { (error: Error?) in
                            if let error = error {
                                print("Error while deleting features : \(error.localizedDescription)")
                            }
                        }
                    }
                }
            }
    
            // Obtain an array of tables managed by the service geodatabase.
            let connectedTables = self.serviceGeodatabase.connectedTables
    
            // Check if the service geodatabase has any local edits?
            if self.serviceGeodatabase.hasLocalEdits() {
    
                // Apply edits to the service for all of the tables in the service
                // geodatabase to ensure that the 'database behavior' is honored.
                self.serviceGeodatabase.applyEdits {(featureTableEditResults: [AGSFeatureTableEditResult]?, error: Error?) in
                    if let error = error {
                        print("Error while applying edits :: \(error.localizedDescription)")
                    }
                }
            }
    
            // Undo the local edits for all the tables in the service geodatabase.
            self.serviceGeodatabase.undoLocalEdits {(error) in
                if let error = error {
                    print("Error while undoing edits :: \(error.localizedDescription)")
                }
            }
    
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    
  3. You can use hasLocalEdits() on the service geodatabase to see if there are unsaved edits in any of the connected tables. You can then use applyEdits() on the service geodatabase to send all edits to the service in a single transaction. This ensures that geodatabase behavior is appropriately leveraged.

    Use dark colors for code blocks
                                                                                                                               
    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
    115
    116
    117
    118
    119
    120
    121
    122
    123
    //
    // Copyright 2022 ESRI
    //
    // All rights reserved under the copyright laws of the United States
    // and applicable international laws, treaties, and conventions.
    //
    // You may freely redistribute and use this sample code, with or
    // without modification, provided you include the original copyright
    // notice and use restrictions.
    //
    // See the use restrictions at http://help.arcgis.com/en/sdk/10.0/usageRestrictions.htm
    //
    
    import UIKit
    import ArcGIS
    
    class ViewController: UIViewController {
        @IBOutlet var mapView: AGSMapView!
    
        var map:AGSMap!
        var serviceFeatureTable: AGSServiceFeatureTable!
        var featureLayer: AGSFeatureLayer!
        var serviceGeodatabase: AGSServiceGeodatabase!
    
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // The feature layer must be loaded from a webmap or from a table in a
            // service geodatabase using the table(withLayerID:) (as shown here).
            self.serviceGeodatabase = AGSServiceGeodatabase(url: URL(string: "https://sampleserver7.arcgisonline.com/server/rest/services/DamageAssessment/FeatureServer")!)
            // NOTE: Never hardcode login information in a production application. This is done solely for the sake of this code snippet.
            self.serviceGeodatabase.credential = AGSCredential(user: "editor01", password: "S7#i2LWmYH75")
            self.serviceGeodatabase.load {[weak self] error in
                guard let self = self else {return}
                if let error = error {
                    print(error.localizedDescription)
                    return
                }
                self.serviceFeatureTable = self.serviceGeodatabase.table(withLayerID: 0)
                self.serviceFeatureTable.load { [weak self] error in
                    guard let self = self else {return}
                    if let error = error {
                        print(error.localizedDescription)
                        return
                    }
                    self.map = AGSMap(basemapStyle: .arcGISLightGray)
                    self.featureLayer = AGSFeatureLayer(featureTable: self.serviceFeatureTable)
                    if let featureLayer = self.featureLayer {
                        self.map.operationalLayers.add(featureLayer)
                        self.mapView.map = self.map
                        self.editData()
                    }
                }
            }
        }
    
        func editData(){
    
    
            // Create a new feature and define its geometry and attributes.
            let newFeature = self.serviceFeatureTable.createFeature()
            newFeature.geometry = AGSPoint(x: 1036000, y: 1860000, spatialReference:self.serviceFeatureTable.spatialReference)
            newFeature.attributes["pstlcity"] = "London"
    
            // Add the new feature to the local table.
            self.serviceFeatureTable.add(newFeature)  {(error: Error?) in
                if let error = error {
                    print("Error", error)
                }
            }
    
            // Use a query to return specific features.
            let queryParams = AGSQueryParameters()
            queryParams.whereClause = "pstlcity = 'London'"
            self.serviceFeatureTable.queryFeatures(with: queryParams) { [weak self] (result, error) in
                guard let self = self else { return }
                if let error = error {
                    print(error.localizedDescription)
                }
                if let features = result?.featureEnumerator().allObjects {
                    if !features.isEmpty {
    
                        // Delete these features.
                        self.serviceFeatureTable.delete(features) { (error: Error?) in
                            if let error = error {
                                print("Error while deleting features : \(error.localizedDescription)")
                            }
                        }
                    }
                }
            }
    
            // Obtain an array of tables managed by the service geodatabase.
            let connectedTables = self.serviceGeodatabase.connectedTables
    
            // Check if the service geodatabase has any local edits?
            if self.serviceGeodatabase.hasLocalEdits() {
    
                // Apply edits to the service for all of the tables in the service
                // geodatabase to ensure that the 'database behavior' is honored.
                self.serviceGeodatabase.applyEdits {(featureTableEditResults: [AGSFeatureTableEditResult]?, error: Error?) in
                    if let error = error {
                        print("Error while applying edits :: \(error.localizedDescription)")
                    }
                }
            }
    
            // Undo the local edits for all the tables in the service geodatabase.
            self.serviceGeodatabase.undoLocalEdits {(error) in
                if let error = error {
                    print("Error while undoing edits :: \(error.localizedDescription)")
                }
            }
    
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    

    The advantage of calling AGSServiceGeodatabase.applyEditsWithCompletion: on the service geodatabase, rather than on each individual table, is that it ensures that all of the edit operations take place in a single transaction. Either all of the edits are applied or none of them. If you apply edits on each table individually, it is possible that only some edits will be applied (for example, if you lose network connectivity in the middle of the operation).

  4. To undo local edits in the connected tables, use undoLocalEdits() on the service geodatabase.

    Use dark colors for code blocks
                                                                                                                               
    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
    115
    116
    117
    118
    119
    120
    121
    122
    123
    //
    // Copyright 2022 ESRI
    //
    // All rights reserved under the copyright laws of the United States
    // and applicable international laws, treaties, and conventions.
    //
    // You may freely redistribute and use this sample code, with or
    // without modification, provided you include the original copyright
    // notice and use restrictions.
    //
    // See the use restrictions at http://help.arcgis.com/en/sdk/10.0/usageRestrictions.htm
    //
    
    import UIKit
    import ArcGIS
    
    class ViewController: UIViewController {
        @IBOutlet var mapView: AGSMapView!
    
        var map:AGSMap!
        var serviceFeatureTable: AGSServiceFeatureTable!
        var featureLayer: AGSFeatureLayer!
        var serviceGeodatabase: AGSServiceGeodatabase!
    
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // The feature layer must be loaded from a webmap or from a table in a
            // service geodatabase using the table(withLayerID:) (as shown here).
            self.serviceGeodatabase = AGSServiceGeodatabase(url: URL(string: "https://sampleserver7.arcgisonline.com/server/rest/services/DamageAssessment/FeatureServer")!)
            // NOTE: Never hardcode login information in a production application. This is done solely for the sake of this code snippet.
            self.serviceGeodatabase.credential = AGSCredential(user: "editor01", password: "S7#i2LWmYH75")
            self.serviceGeodatabase.load {[weak self] error in
                guard let self = self else {return}
                if let error = error {
                    print(error.localizedDescription)
                    return
                }
                self.serviceFeatureTable = self.serviceGeodatabase.table(withLayerID: 0)
                self.serviceFeatureTable.load { [weak self] error in
                    guard let self = self else {return}
                    if let error = error {
                        print(error.localizedDescription)
                        return
                    }
                    self.map = AGSMap(basemapStyle: .arcGISLightGray)
                    self.featureLayer = AGSFeatureLayer(featureTable: self.serviceFeatureTable)
                    if let featureLayer = self.featureLayer {
                        self.map.operationalLayers.add(featureLayer)
                        self.mapView.map = self.map
                        self.editData()
                    }
                }
            }
        }
    
        func editData(){
    
    
            // Create a new feature and define its geometry and attributes.
            let newFeature = self.serviceFeatureTable.createFeature()
            newFeature.geometry = AGSPoint(x: 1036000, y: 1860000, spatialReference:self.serviceFeatureTable.spatialReference)
            newFeature.attributes["pstlcity"] = "London"
    
            // Add the new feature to the local table.
            self.serviceFeatureTable.add(newFeature)  {(error: Error?) in
                if let error = error {
                    print("Error", error)
                }
            }
    
            // Use a query to return specific features.
            let queryParams = AGSQueryParameters()
            queryParams.whereClause = "pstlcity = 'London'"
            self.serviceFeatureTable.queryFeatures(with: queryParams) { [weak self] (result, error) in
                guard let self = self else { return }
                if let error = error {
                    print(error.localizedDescription)
                }
                if let features = result?.featureEnumerator().allObjects {
                    if !features.isEmpty {
    
                        // Delete these features.
                        self.serviceFeatureTable.delete(features) { (error: Error?) in
                            if let error = error {
                                print("Error while deleting features : \(error.localizedDescription)")
                            }
                        }
                    }
                }
            }
    
            // Obtain an array of tables managed by the service geodatabase.
            let connectedTables = self.serviceGeodatabase.connectedTables
    
            // Check if the service geodatabase has any local edits?
            if self.serviceGeodatabase.hasLocalEdits() {
    
                // Apply edits to the service for all of the tables in the service
                // geodatabase to ensure that the 'database behavior' is honored.
                self.serviceGeodatabase.applyEdits {(featureTableEditResults: [AGSFeatureTableEditResult]?, error: Error?) in
                    if let error = error {
                        print("Error while applying edits :: \(error.localizedDescription)")
                    }
                }
            }
    
            // Undo the local edits for all the tables in the service geodatabase.
            self.serviceGeodatabase.undoLocalEdits {(error) in
                if let error = error {
                    print("Error while undoing edits :: \(error.localizedDescription)")
                }
            }
    
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    

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