Monday, March 29, 2010

Avoiding ConcurrentModificationException in MapView.draw()

Throwing this out there because it may help someone. For whatever reason, you may have gotten the idea that you would like to add and remove Overlays to your MapView as you see fit. Problem is, that MapView may be iterating through that List when you pull the rug out from under it and remove something from that List. Adding is okay of course.

Here's my advice, don't ever remove from it. Just render it ineffective. You may find it best to just empty the collection that backs your ItemizedOverlay. Or override draw() and add a hook to return early without doing anything in a particular circumstance. Of course, to do any of this you'll need to maintain a handy reference to the Overlay you add.

So remember, it's okay to call mapView.getOverlays.add() at any time, but don't call mapView.getOverlays.remove(), because if the timing is off, it will blow up on you.

While I'm writing this, I was thinking, you may be able to call remove(), by first disabling your MapView. That's worth a shot if it works in your scenario.

No comments:

Post a Comment