Feature Layer Query never enters delegate methods

4190
2
12-19-2013 01:48 PM
StevenKitterman1
New Contributor II
I am trying to load in a bunch of features from a feature service on arcgis.com. I know the service is coming in just fine because it renders on the map. What I can't get it to do is to let me read out the features themselves.

To my knowledge, I need to make a query on this feature layer and then I can parse out the results in a delegate method. However, my delegate method is never getting called, making me wonder if I am missing a step somewhere.

Below is my code:

 (void)viewDidLoad {
    [super viewDidLoad];
    
    AGSLocalTiledLayer *localLayer = [AGSLocalTiledLayer localTiledLayerWithName:@"myname"];
    [self.mapView addMapLayer:localLayer withName:@"Campus Basemap"];
   
    NSURL *url = [NSURL URLWithString:@"myservicename];
    AGSFeatureLayer *buildingPoints = [AGSFeatureLayer featureServiceLayerWithURL:url mode:AGSFeatureLayerModeOnDemand];
    [self.mapView addMapLayer:buildingPoints withName:@"BYU Buildings"];
    
    //set myself as the query's delegate
    buildingPoints.queryDelegate = self;
    
    AGSQuery *query = [AGSQuery query];
    //no "where" clause because I want all the records
    query.outFields = [NSArray arrayWithObjects:@"FID", @"Abbr", @"Name", nil];
    [buildingPoints selectFeaturesWithQuery:query selectionMethod:AGSFeatureLayerSelectionMethodNew];
    
    [self.mapView.locationDisplay startDataSource];
}

- (void)featureLayer:(AGSFeatureLayer *)featureLayer operation:(NSOperation *)op didQueryFeaturesWithFeatureSet:(AGSFeatureSet *)featureSet {
    //IT NEVER GETS IN HERE!
    //populate all the points into an nsarray.
    for (AGSGraphic *selectedFeature in featureSet.features){
        Building *building = [[Building alloc] init];
        building.fid = [[selectedFeature attributeForKey:@"FID"] integerValue];
        building.abbreviation = [selectedFeature attributeAsStringForKey:@"Abbr"];
        building.fullName = [selectedFeature attributeAsStringForKey:@"Name"];
    }
}

- (void)featureLayer:(AGSFeatureLayer *)featureLayer operation:(NSOperation *)op didFailSelectFeaturesWithError:(NSError *)error {
    //IT NEVER GETS HERE EITHER
    NSLog(@"Query Failed.");
}




Any ideas?
0 Kudos
2 Replies
NimeshJarecha
Esri Regular Contributor
It's because your local variable of AGSFeatureLayer get released before delegate method is called. Either create an instance variable for AGSFeatureLayer or create strong/retain property.

Regards,
Nimesh
0 Kudos
HrishikeshPol
New Contributor

Hey Nimesh,

I have created instance variable for AGSFeatureLayer but still facing some issues. Below is the code from which I'm uploading 3 feature layers on basemap. I'm able to load it but it is not consistent.

Please let me know if I'm making some mistake. Thank you in advance.

    // Add basemap.

    NSURL* url = [NSURL URLWithString: @"http://services.arcgisonline.com/arcgis/rest/services/World_Topo_map/mapserver"];

    AGSTiledMapServiceLayer* layer = [AGSTiledMapServiceLayer tiledMapServiceLayerWithURL: url];

    layer.delegate = self;

    [self.mapView addMapLayer:layer withName:@"Streets"];

    //zoom to an area

    AGSEnvelope *envelope = [AGSEnvelope envelopeWithXmin:-1.34732920649E7 ymin:4533851.309500001 xmax:-1.3431785844E7

  ymax:4641521.468800001  spatialReference:self.mapView.spatialReference];

    [self.mapView zoomToEnvelope:envelope animated:NO];

   

    // Structures.

    NSString *urlString = [self.currentWorkOrder.detectionURL substringToIndex:[self.currentWorkOrder.detectionURL length]-1];

    self.featureLayerURL0 = [NSURL URLWithString:[NSString stringWithFormat:@"%@0",urlString]];

   

    self.featureLayer0 = [AGSFeatureLayer featureServiceLayerWithURL:self.featureLayerURL0 mode:AGSFeatureLayerModeOnDemand];

    self.featureLayer0.delegate = self;

    [self.mapView addMapLayer:self.featureLayer0 withName:@"CloudData0"];

   

    // Alignment.

    self.featureLayerURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@1",urlString]];

   

    self.featureLayer = [AGSFeatureLayer featureServiceLayerWithURL:self.featureLayerURL mode:AGSFeatureLayerModeOnDemand];

    self.featureLayer.delegate = self;

    [self.mapView addMapLayer:self.featureLayer withName:@"CloudData"];

   

    // Detections.

    self.featureLayerURL1 = [NSURL URLWithString:[NSString stringWithFormat:@"%@2",urlString]];

   

    self.featureLayer1 = [AGSFeatureLayer featureServiceLayerWithURL:self.featureLayerURL1 mode:AGSFeatureLayerModeOnDemand];

    self.featureLayer1.delegate = self;

    PO(self.featureLayer1.fields);

    [self.mapView addMapLayer:self.featureLayer1 withName:@"CloudData1"];

   

    self.featureLayer1.queryDelegate = self;

    AGSQuery *query = [AGSQuery query];

    //no "where" clause because I want all the records

    query.outFields = [NSArray arrayWithObjects:@"Detection", @"Health", @"Circuit", nil];

    [self.featureLayer1 selectFeaturesWithQuery:query selectionMethod:AGSFeatureLayerSelectionMethodNew];

   

    [self.mapView.locationDisplay startDataSource];

0 Kudos