In my app I need to draw many placemarks and continuously update their location (or delete them if they become old). Now I'm doing it with some cascade and sequential loops, and my fear is that this is not the most efficient way to do it. Here it is how I do this work up to now:
- For each placemark to draw I first search in MapKit annotations array (made by a custom MKPlacemark where I added the creation date and a unique identifier) for the unique id of the placemark to draw. If I find the placemark I update only its location, otherwise I add the new annotation on the map.
- For each placemark in the MapKit annotations array, I check if the elapsed time between now and the creation date is greater than 10 seconds and if it is true, I remove that annotation from the map.
I repeat these 2 loops each 2 seconds.
The problem is that when I manage 20 placemarks or more, the loops take too much time and the update-remove process doesn't work as expected.
Which is the fastest method to work with annotations array? Is it faster removing each annotation and redrawing them all?
I solved the problem. I was doing it in the right way. The only remaining problem was the freeze of the screen during map update. For this reason i solved running the update/delete process on another thread. This means that when you are ready to update the placemark or to delete it, your background thread needs to call the map update on the main thread (the map must be updated on the main thread).