Select to view content in your preferred language

Querying feature layer when it loads.

1525
2
Jump to solution
04-21-2022 11:10 PM
YogeshSwami
Emerging Contributor

Hello

I am trying to query my feature layer when it gets loaded. I found this method on the esri website:

// returns all the graphics from the layer view
view.whenLayerView(layer).then(function(layerView){
layerView.watch("updating", function(val){
if(!val){ // wait for the layer view to finish updating
layerView.queryFeatures().then(function(results){
console.log(results); // prints all the client-side features to the console
});
}
});
});

 

But this queries through all the features everytime layer gets updated(like dragging, zooming etc.). I just want to query once when it gets loaded. Can someone please help me out with this?

Thanks in advanced

 

 

0 Kudos
1 Solution

Accepted Solutions
UndralBatsukh
Esri Regular Contributor

Hi there, 

What @KenBuja said. If you are using version 4.23, then please use reactiveUtils instead of watchUtils. 

 

// this will only run once after layerview finishes updating for the first time
view.whenLayerView(layer).then((layerView) => {
   reactiveUtils.whenOnce(() => !layerView.updating).then(() => {
     console.log("layer finished updating");
   });
});

View solution in original post

2 Replies
KenBuja
MVP Esteemed Contributor

Take a look at this useful blog by @ReneRubalcava about doing things when a layer loads.

async function whenDone() {
  const layerView = await view.whenLayerView(featureLayer);
  await watchUtils.whenFalseOnce(layerView, 'updating');
  console.log('layerView is done loading and drawing', layerView);
}

Most of the time, I'm interested in just the first time the layer loads in an application, so I use whenFalseOnce of the watchUtils to check the first time it happens. If you are interested in every time the LayerView is done updating, you could use whenFalse to keep an eye on it.

 

UndralBatsukh
Esri Regular Contributor

Hi there, 

What @KenBuja said. If you are using version 4.23, then please use reactiveUtils instead of watchUtils. 

 

// this will only run once after layerview finishes updating for the first time
view.whenLayerView(layer).then((layerView) => {
   reactiveUtils.whenOnce(() => !layerView.updating).then(() => {
     console.log("layer finished updating");
   });
});