Select to view content in your preferred language

Calling queryTask in a loop

3020
4
10-27-2011 08:25 AM
MiriEshel
Esri Contributor
Hi,

I'm trying to call queryTask in a loop but it looks like it refers only to the last call, i.e. it enters to didExecuteWithFeatureSetResult only in the last call).

Is there a way to force it to work asynchronously?

Here is the chunk of code I'm using:

  for (NSDictionary *r in res)
  {
   NSString *layer = [r objectForKey:@"layerId"]  ;
     
   NSString *objectsID = @"";
  
   NSArray *features = [r objectForKey:@"features"];
   for (NSDictionary *feature in features) {
    NSString *objectID = [feature objectForKey:@"OBJECTID"];
    objectsID = [NSString stringWithFormat:@"%@ , %@",objectsID, objectID];
   }
   //Remove first ,
   objectsID = [objectsID substringFromIndex:2];

   //objectsID = @"OBJECTID IN (";
   objectsID = [NSString stringWithFormat:@"%@  %@", @"OBJECTID IN (", objectsID];
   objectsID = [NSString stringWithFormat:@"%@ )",objectsID];

   NSString *waterLayerURL =[NSString stringWithFormat:@"%@%@",@"http://mnatania10/ArcGIS/rest/services/GeneralView/MapServer/", layer];
  
   //set up query task against layer, specify the delegate
   self.queryTask = [AGSQueryTask queryTaskWithURL:[NSURL URLWithString:waterLayerURL]];
   self.queryTask.delegate = self;
  
   //return all fields in query
   self.query.where = objectsID;
  
   NSLog(@"name : %@", self.query.where);
   [self.queryTask executeWithQuery:self.query];
     
 
  }

Thanks a lot,
Miri
0 Kudos
4 Replies
RickJones
Deactivated User
In your for loop, objectsID is being reset each time, not appended to. Is this what you wanted?

When the loop finishes, only the last value will be in the variable.
0 Kudos
NimeshJarecha
Esri Regular Contributor
Are you querying by where clause or objectIds? The code "self.query.where = objectsID;" seems incorrect.

Please attach a sample project, that'll help understand your code better.

Regards,
Nimesh
0 Kudos
ShaharBukra
Regular Contributor
Hi Nimesh,
Im working with miri...

we trying to do it by using where clause
the "objectids" you see is string with long query in it.

we try do it asnyc, but cant find how.

thanks,
have a great week

Shahar.
0 Kudos
DiveshGoyal
Esri Regular Contributor
One reason why you may be experiencing the problem you describe is because you may be allocating the query task again and again in the loop.  The following line of code -

self.queryTask = [AGSQueryTask queryTaskWithURL:[NSURL URLWithString:waterLayerURL]];

will release the previous instance of query task and create a new one. When the previous one is released, it will no longer invoke the delegate. (for more information, refer to apple's doc on objective-C properties)

If all your queries are against the same service, you should create the task only once, and call executeWithQuery: in a loop for each query.
0 Kudos