Related Records Query

3262
5
05-15-2012 06:52 AM
ChristopherEbright
New Contributor III
I am having trouble implementing AGSRelationshipQuery, I get a successful response, but the data returned id not what I expect and I am not sure if I am setting up my query the correct way. What I am trying to do is query related records from a point feature class (through a map service) that has a relationship set up with a table.
Here is code: (the names of the server and services have been changed to protect the innocent, and unfortunately, this server is not public so i can't point anybody to it)

- (void)mapView:(AGSMapView *)mapView didClickCalloutAccessoryButtonForGraphic:(AGSGraphic *)graphic {

    self.queryTask = [AGSQueryTask queryTaskWithURL:[NSURL URLWithString:@"http://<myServer>/arcgis/rest/services/<myFolder>/<myService>/MapServer/0"]];
    self.queryTask.delegate = self;
   
    self.relQuery = [AGSRelationshipQuery relationshipQuery];
    self.relQuery.objectIds = [NSArray arrayWithObject:[graphic.attributes objectForKey:@"OBJECTID"]];
    self.relQuery.outFields = [NSArray arrayWithObject :@"*"];
    self.relQuery.relationshipId = 0;

    [self.queryTask executeWithRelationshipQuery:self.relQuery];
{

- (void) queryTask:(AGSQueryTask *)queryTask operation:(NSOperation*)op didExecuteWithRelatedFeatures:(NSDictionary *)relatedFeatures {

   NSLog(@"relatedFeatures: %@", relatedFeatures);

}


The response I get back from the NSLog on related Features is:

relatedFeatures: (
    "AGSFeatureSet: display name: (null)
\ngeometry type: esriGeometryPoint
\nnum features: 1
\nspatial reference: (null)"
)


I have confirmed the parameters in my query are valid using the rest/services web page, and there I get a json object that has the data I expect. But I am not sure what to make of the response I am getting through the iOS API.
I have also tested the a regular http request using the direct URL for the REST query, and I do get back the proper response,

    NSString *relatedQueryString = @"http://<myServer>/ArcGIS/rest/services/<myFolder>/<myService>/MapServer/0/queryRelatedRecords?objectIds=1&relationshipId=0&definitionExpression=&returnGeometry=false&maxAllowableOffset=&outSR=&outFields=*&f=pjson";
    NSError *theError = nil; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:relatedQueryString]];
    NSURLResponse *theResponse = [[NSURLResponse alloc] init];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];
    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSDictionary *jsonDict = [string AGSJSONValue];



so I am figuring I am just not setting up the related request correctly.
Chris
0 Kudos
5 Replies
DiveshGoyal
Esri Regular Contributor
Are you sure that the graphic you're getting back in the didClickCalloutAccessoryButtonForGraphic: parameter has any related records?
For example, if you have multiple layers in your map, the graphic could belong to a completely different layer.
Just to rule out such problems, I would try running the query by hardcoding the objectid (instead of getting it from the graphic's attributes). If things still don't work, we can troubleshoot more.
0 Kudos
ChristopherEbright
New Contributor III
Good thought, but still did not work. I am logging the objectid and it is returning the correct one, and if i compare to a manual rest query, the related records are there.
0 Kudos
NimeshJarecha
Esri Regular Contributor
Chris,

Could you please print your result with this code and check? Also, in your query, please set self.relQuery.returnGeometry = TRUE if you want the geometry.

for(NSString *aKey in relatedFeatures)
{
 NSLog(@"Key: %@ Value: %@", aKey, [relatedFeatures objectForKey:aKey]);
 AGSFeatureSet *featureSet = [relatedFeatures objectForKey:aKey];
   
 for (int i=0; i< [featureSet.features count]; i++) {
  AGSGraphic *graphic = [featureSet.features objectAtIndex:i];
  NSLog(@"graphic: %@",graphic);
 }
}


Regards,
Nimesh
0 Kudos
ChristopherEbright
New Contributor III
Here is what I got:

2012-05-18 11:08:33.738 WPC_Mobile[7173:707] Key: 6675 Value: AGSFeatureSet: display name: (null)

geometry type: esriGeometryPoint

num features: 1

spatial reference: (null)
2012-05-18 11:08:33.741 WPC_Mobile[7173:707] graphic: geometry: (null), symbol: (null), attributes: {
    CompletionRecID = 10214;
}, visible: 1


This is what I was looking for. It looks like I failed to go into the FeatureSet.features and pull out the attributes from the graphic.
Thank you very much!

Chris
0 Kudos
NimeshJarecha
Esri Regular Contributor
Great! Glad to know that you got what you wanted! 🙂

Regards,
Nimesh
0 Kudos