So I've created an AGSServiceFeatureTable and trying to populate it with features based on a query, however it doesn't seem to be completing. I don't get an error or anything and watching the url requests in Charles show's no network requests being made. The endpoint url and whereClause query I'm using is the same as a separate table i've used as a source for a Feature Layer on the map and it draws correctly. Am I missing something here (likely! haha) or is something else wrong here?
func getCountsInGeometry(creds: AGSCredential?, polygon: AGSGeometry?, bufferPoint: AGSPoint, query: String) {
print("getting counts")
guard let polygon = polygon as? AGSPolygon else {
return
}
let table = AGSServiceFeatureTable(url: URL(string: PGRLAYERURL)!)
table.featureRequestMode = .manualCache
table.credential = creds
let queryParams = AGSQueryParameters()
queryParams.geometry = polygon
queryParams.spatialRelationship = .intersects
queryParams.returnGeometry = true
queryParams.whereClause = query
table.populateFromService(with: queryParams, clearCache: true, outFields: ["*"]) { (results, error) in
print("done populateFromService")
guard error == nil else {
print("Error \(error?.localizedDescription)")
return
}
print(results?.featureEnumerator().allObjects.count)
}
}
Solved! Go to Solution.
Hello Todd,
`table` is declared as a local variable in the method and therefore goes out of scope by the time the query is complete and the call back can be fired. That's probably why you don't see anything.
You can declare the table as a property on your class so it's retained.
A second option is you can access the table/its properties within the query call back, so it's held on to without being released. You may not have a reason to do this in which case the first option is recommended where the table is defined at a broader scope so it can be retained
Option 1:
class VC {
let table = AGSServiceFeatureTable(url: URL(string: PGRLAYERURL)!)
func getCountsInGeometry(creds: AGSCredential?, polygon: AGSGeometry?, bufferPoint: AGSPoint, query: String) {
...
}
}
Option 2:
table.populateFromService(with: queryParams, clearCache: true, outFields: ["*"]) { (results, error) in
// Access table or its properties here, so it can be retained.
...
}
When you create a layer from a table, and add the layer to a map, the map holds on its layers (and the layer to its backing table), and that's why you don't see the issue in that case.
Hope that helps!
Suganya
Hello Todd,
`table` is declared as a local variable in the method and therefore goes out of scope by the time the query is complete and the call back can be fired. That's probably why you don't see anything.
You can declare the table as a property on your class so it's retained.
A second option is you can access the table/its properties within the query call back, so it's held on to without being released. You may not have a reason to do this in which case the first option is recommended where the table is defined at a broader scope so it can be retained
Option 1:
class VC {
let table = AGSServiceFeatureTable(url: URL(string: PGRLAYERURL)!)
func getCountsInGeometry(creds: AGSCredential?, polygon: AGSGeometry?, bufferPoint: AGSPoint, query: String) {
...
}
}
Option 2:
table.populateFromService(with: queryParams, clearCache: true, outFields: ["*"]) { (results, error) in
// Access table or its properties here, so it can be retained.
...
}
When you create a layer from a table, and add the layer to a map, the map holds on its layers (and the layer to its backing table), and that's why you don't see the issue in that case.
Hope that helps!
Suganya
Suganya, thanks that was it.