SDK 100.6 - How to perform a synchronous Query?

719
3
Jump to solution
11-19-2019 07:01 AM
OscarDiago_Alonso
New Contributor III

Hi.

I need to migrate an Android application developed with the ESRI SDK 10.2.8 to the newest SDK: 100.6.

My first big obstacle is migrating all the queries. Currently, they are done with the object QueryTask (which doesn't exist any longer). With it, I get the response from the server, I send the data with a return to the next step.

Now, it seems that all queries are asynchronous, so the data that is returned is always empty. How can I get a synchronous behaviour with the new SDK? I'm totally stuck.

ArrayList<Community> communities = new ArrayList<Community>();

QueryParameters query = new QueryParameters();
				
// WHERE
StringBuilder whereStrBuilder = new StringBuilder();
whereStrBuilder.append("display_order > 0");
query.setWhereClause(whereStrBuilder.toString());
			
// ORDER		
OrderBy order = new OrderBy("display_order", SortOrder.ASCENDING);
query.getOrderByFields().add(order);
					
query.setReturnGeometry(false);

			
final ListenableFuture<FeatureQueryResult> future = Layer.SERVICE_COMMUNITY.queryFeaturesAsync(query, ServiceFeatureTable.QueryFeatureFields.LOAD_ALL);

future.addDoneListener(new Runnable() {
	                @Override
	                public void run() {
	                  try {

	                    // check if server result successful
	                	FeatureQueryResult result = future.get();
	                	// check there are some results
	      		        Iterator<Feature> resultIterator = result.iterator();
	      		      while (resultIterator.hasNext()) {
	      		    	Feature feature = resultIterator.next();
	      		    	communities.add(featureToCommunity(feature));
	      		      }
	                  } catch (Exception e) {

	                  }
	                }
	              });

return communities;

communities is always empry, because it is returned before the addDoneListener is triggered. Is there a way of making the process wait until the query has finished so the proper list of communities is returned?

Thanks for any help provided!

0 Kudos
1 Solution

Accepted Solutions
MarkBaird
Esri Regular Contributor

A future is a promise to return something.  You can use this asynchronously as you've show above and listen into a callback to tell you the result is ready.  This will call back on a separate thread.

Futures can also be made to block the thread you are on until the result comes through.  If you don't bother listening to the done event, you can just call future.get.

However be careful that you are not using this on the UI thread of your app!!! 

View solution in original post

3 Replies
OscarDiago_Alonso
New Contributor III

I will answer myself:

final ListenableFuture<FeatureQueryResult> future = Layer.SERVICE_COMMUNITY.queryFeaturesAsync(query, ServiceFeatureTable.QueryFeatureFields.LOAD_ALL);

// check if server result successful
FeatureQueryResult result = future.get();
// check there are some results
Iterator<Feature> resultIterator = result.iterator();
while (resultIterator.hasNext()) {
      Feature feature = resultIterator.next();
      communities.add(featureToCommunity(feature));
}

return communities;
0 Kudos
MarkBaird
Esri Regular Contributor

A future is a promise to return something.  You can use this asynchronously as you've show above and listen into a callback to tell you the result is ready.  This will call back on a separate thread.

Futures can also be made to block the thread you are on until the result comes through.  If you don't bother listening to the done event, you can just call future.get.

However be careful that you are not using this on the UI thread of your app!!! 

OscarDiago_Alonso
New Contributor III

Thanks for your answer!! I made it work

The migration to the new SDK is being more of a headache than I was expecting.

0 Kudos