removeAllGraphics + addGraphic + dataChanged

726
6
Jump to solution
09-24-2012 09:39 AM
AaronConnolly
Occasional Contributor
Hello,

I have a question regarding the dataChanged method on the AGSGraphicsLayer. My application removes all the graphics, then adds graphics one by one from a list, then calls dataChanged on the layer. Is it expected that dataChanged should pick up all the changes made to the layer? By that I mean, should I expect my old graphics to be removed and the new graphics added after the call to dataChanged? Or do I need to call dataChanged after I remove OR add graphics, each time?

The reason I ask is that something weird is going on and the single call to dataChanged doesn't refresh my map. If I change my code to look like this:

removeAllGraphics;
dataChanged;  // <-- Added extra call

addGraphic;
dataChanged;

The map will refresh properly. Odd, right? What's the expected behavior here?

Thanks
- Aaron
0 Kudos
1 Solution

Accepted Solutions
AaronConnolly
Occasional Contributor
The problem is that the graphics layer won't redraw if it is hidden. The graphics layer's view must be visible before dataChanged can be expected to do it's job. At least that's how I was able to solve the problem (with Nimesh' help of course).

View solution in original post

0 Kudos
6 Replies
NimeshJarecha
Esri Regular Contributor
A single call to dataChanged should refresh the map. That is an expected behavior. Could you please attach a sample application/xcode project here so I can test, why you have to call dataChanged twice?

Regards,
Nimesh
0 Kudos
AaronConnolly
Occasional Contributor
Hi Nimesh,

It will take me a few minutes to build a sample project but for now I can provide a little bit of insight into what I'm doing and perhaps you can tell me if there's something I should be wary of.

Here's the algorithm:

1. User builds a query with some attributes in separate view
2. User switches back to map view and the map is told to query an API endpoint for results based on the query attributes in step 1.
3. View controller waits for results to return from API endpoint. When they do the map's graphics layer has its graphics cleared, and new graphics are added one by one as the results are parsed.
4. After the clear and individual adds have completed the dataChanged is called once.

The problem is that when my application returns to the map view and the API endpoint call finishes, the map does not refresh with a single call to dataChanged. The map has to be panned again (and the algorithm is repeated) before the points show up. I know that the API call completes because of other code that is executed.

Here's some of the code from the API endpoint complete delegate method, with a few lines here and there stripped out:

        NSDictionary *parsedData = [responseString AGSJSONValue];
        
        self.mapCrimePoints = [parsedData objectForKey:@"Records"];
        maxValuesReached = [[parsedData objectForKey:@"ReachedMaxValues"] boolValue];
        
        [self.crimeGraphicsLayer removeAllGraphics];
        //[self.crimeGraphicsLayer dataChanged];            // Shouldn't be necessary

        [self.crimeGraphicTemplates removeAllObjects];
        
        NSMutableArray *crimeHash = [NSMutableArray array];
        NSMutableDictionary *helperDict = [NSMutableDictionary dictionary];
        
        for (NSDictionary *crime in self.mapCrimePoints) {
            
            // create hash of points by x,y
        }
        
        for(NSArray *crimesArray in crimeHash) {
            
            // create AGSGraphic and add to graphics layer            
        }        
        
        //Tell the layer to redraw itself
        [self.crimeGraphicsLayer dataChanged];
        self.crimeGraphicsView.hidden = NO;



Using the code above the second call to dataChanged above is the only way to ensure that the points on the map are redrawn. Any thoughts?

Thanks,
- Aaron
0 Kudos
NimeshJarecha
Esri Regular Contributor
Based on description, you seems to be doing correct thing and one call of dataChanged should work. When you call dataChanged, map is in view or some other view?

Regards,
Nimesh
0 Kudos
AaronConnolly
Occasional Contributor
The map is top most view and is visible.
0 Kudos
NimeshJarecha
Esri Regular Contributor
Then one call of dataChanged should work. Please send repro case and I'll look into it.

Regards,
Nimesh
0 Kudos
AaronConnolly
Occasional Contributor
The problem is that the graphics layer won't redraw if it is hidden. The graphics layer's view must be visible before dataChanged can be expected to do it's job. At least that's how I was able to solve the problem (with Nimesh' help of course).
0 Kudos