Attributes results dosen't match fields count in feature layer query

796
2
12-03-2017 04:05 PM
ShengchuanZhou
New Contributor II

Dear All,

I am working on a ios app recently and I checked the arcgis-ios-sdk-samples for some help.

In the Feature layer selection demo, I got a question about feature layer attributes. When the query results returned, the  following print output

print("\(result.featureEnumerator().allObjects.count) feature(s) selected")

print(result.featureEnumerator().nextObject()?.attributes)

shows only 2 attributes fields (objectid, typedamage) and values, but in the feature definition, the feature contains 4 attribute fields (objectid, typedamage, numoccup, descdamage). 

Since i do not find outfields property or other ways to get other attributes. Are there any way to get the entire attributes list? 

Many thanks!

0 Kudos
2 Replies
SuganyaBaskaran1
Esri Contributor

When we query for features, by default, only the minimum required attributes required to render it are fetched. The feature is said to be in an "unloaded" state. This behavior is because a service could have any number of fields (hundreds if not thousands), and all these fields are not necessary for rendering the feature on the map or selecting it. So in order to get all the fields, we need to "load" the feature. 

Option 1: 

You can add the following to the sample code to load the features

self.featureLayer.selectFeatures(withQuery: queryParams, mode: AGSSelectionMode.new) { (queryResult:AGSFeatureQueryResult?, error:Error?) -> Void in
   if let error = error {
      print(error)
   }
            
   if let result = queryResult {
      let featureEnumerator = result.featureEnumerator()
      while featureEnumerator.hasNextObject() {
         let feature = result.featureEnumerator().nextObject() as! AGSArcGISFeature
         feature.load(completion: { (featureLoadError) in
             print("feature attributes \(feature.attributes)") //this would print all attributes defined.
          })
       }             
   }
}
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Option 2: 

Alternatively, there is a query method on the `AGSServiceFeatureTable` that can return loaded features (by specifying `queryFeatureFields` param to `.loadAll`. You can then select the features as below: 

self.featureTable = self.featureLayer.featureTable as! AGSServiceFeatureTable
self.featureTable.queryFeatures(with: queryParams, queryFeatureFields: .loadAll) { (queryResult:AGSFeatureQueryResult?, error:Error?) in
            
    if let result = queryResult {
       //All features are already loaded here
       print("\(result.featureEnumerator().allObjects.count) feature(s) selected")
       print(result.featureEnumerator().nextObject()?.attributes) //contains all attributes
                
        //Now select the features
        self.featureLayer.select(result.featureEnumerator().allObjects)
     }
            
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

More on "loadable" pattern can be found here

Thanks,

Suganya

0 Kudos
ShengchuanZhou
New Contributor II

Hi Suganya,

Thanks for your reply! It solved my problem perfectly!

Best,

Shengchuan

0 Kudos