What is the difference between QueryFeaturesAsync and PopulateFromServiceAsync?

1052
3
02-13-2020 08:49 AM
ChrisSeabrooke1
New Contributor III

I'm doing a query on a hosted feature layer. The query includes dates but when I use QueryFeaturesAsync the FeatureQueryResult has zero results (even though there are records matching the criteria). I tested with PopulateFromServiceAsync and get results with the same query parameters. I'm curious about the difference between the two methods. Is there a specific scenario where one should be used instead of the other?

Tags (1)
0 Kudos
3 Replies
ThadTilton
Esri Contributor

Hey Chris -

This is an excellent question and something we need to document better. When you run QueryFeaturesAsync, you're querying the local version of the data. Depending on your caching mode (ServiceFeatureTable.FeatureRequestMode), this may not include all the features available in the service. PopulateFromServiceAsync, on the other hand, runs the query on the feature service and populates your local version of the table with the results.

I hope that helps! Thad

0 Kudos
dotMorten_esri
Esri Notable Contributor

I'd like to add a little extra context to what Thad said.

Typically you'd only use the populate method if your mode is "Manual". This mode is used where you just want to fetch a set of data once, and not have the mapview automatically fetch data as you zoom and pan around. But if you set it to manual, there won't be any data, so you'd populate the local cache with this method.

The query is what you use if you want to query for data. If you're in manual mode, only the local cache is queried. If you're not, then the query goes to the server to fetch the freshest version of the data.

0 Kudos
DavidSchuster
Occasional Contributor

Thank you for posting this question. Can you explain a bit more about what to expect in the return value for PopulateFromServiceAsync vs QueryFeaturesAsync if the feature layer that I'm querying from doesn't have an Object ID field defined?

In my situation (using ArcGIS Runtime 100.7), I have created two ServiceFeatureTable objects, each set to ManualCache request mode and pointing to a couple of different feature layers on ArcGIS server.  I'm using these to periodically poll for data.  I'm seeing different behavior between the two, and I think it has to do with the presence (or lack of) an Object ID field.

The first layer has an object ID field, and PopulateFromServiceAsync returns the results as expected.

The second layer does not have an object ID field, and PopulateFromServiceAsync returns no results, even though I know there are results on the server.  However, QueryFeaturesAsync correctly returns the results I was expecting, but ONLY IF I call PopulateFromServiceAsync first.  Not sure if it makes any difference, but I have the clearCache parameter set to true on the call to PopulateFromServiceAsync.

It seems like internally the ServiceFeatureTable is populating the data, but isn't returning those results if the layer doesn't have an Object Id field defined.  This was what threw me off.

My solution was to call PopulateFromServiceAsync, then do a check to see if the ServiceFeatureTable.ObjectIdField is blank, and if so, call QueryFeaturesAsync to get the data in the table.

Does this seem like a good approach (in lieu of the ideal solution of adding an Object Id field)?

Thanks.

0 Kudos