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!
Solved! Go to Solution.
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!!!
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;
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!!!
Thanks for your answer!! I made it work
The migration to the new SDK is being more of a headache than I was expecting.