[GIS] Changing Layer Definitions / Refresh Not Behaving in ArcGIS API for JavaScript

arcgis-javascript-api

In ArcGIS Javascript API 3.3, I have an ArcGISDynamicMapService layer in my application that points to a fairly simple (6 layers) map service. In the application, there is a GUI/widget that allows the user to pick various options – which then dynamically changes the layer's layerDefinitions property to re-render the layer based on their selection.

After I define and set the layerDefinitions of the layer, I call the layer.refresh method to re-render the layer. However, the re-rendered layer is always wrong – either the wrong layers are rendered, or wrong features are rendered, and there doesn't appear to be any pattern in these errors that I can determine. However, once the map is panned or zoomed, the re-rendered layer is drawn exactly as expected. Below is the relevant javascript code. The "format" function is a .NET-like string formatting function I've added to the script.

var layerDefinitions = [];
layerDefinitions[1] = "TransmitterId IN ({0})".format(visibleTransmitterIds.join(","));
layerDefinitions[2] = "TransmitterId IN ({0})".format(visibleTransmitterIds.join(","));
analysisResultLyr.setLayerDefinitions(layerDefinitions);
analysisResultLyr.refresh();

Using various http sniffing tools (Google Developer tools is preferred choice), I can see that the outgoing http request for my layer of interest is exactly the same between the refresh call – and the pan/zoom call. I have also verified the sql contained in the layer definitions, and am positive the layers/features returned in the refresh call are definitely wrong.

Anyone have any ideas as what may be causing this? Better yet, does anyone know an alternate way to refresh a layer without panning/zooming the map?

A couple of other notes:

  1. I have disabled cacheing at the client via setDisableClientCaching
  2. My sql where clauses make use of IN statement. Is this a bad idea with file geodatabases?

Best Answer

I believe that analysisResultLyr.setLayerDefinitions(layerDefinitions); will trigger a refresh automatically after updating the layer definitions see https://developers.arcgis.com/javascript/3/jsapi/arcgisdynamicmapservicelayer-amd.html#setlayerdefinitions. I think your call to refresh may be interfering with some async behaviour with the layer definition updates. Try removing it.