Curious, I added the NSCacheDelegate to see when an item is evicted (I was concerned about having a NSCache for each WMSOverlayView
and how they would behave and memory usage, I set the cache to 32mb and don't want to gobble up all memory). I tried naming each cache so I could see if there were two caches, I could see if the old cache was cleared out to make room in the memory for the newer cache. Realized each cache was always named the same:
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
if (self.wmsOverlayView == nil) {
self.wmsOverlayView = [[WMSOverlayView alloc] initWithOverlay:overlay withCacheName:@"CACHE!"];
}
else {
self.wmsOverlayView = [[WMSOverlayView alloc] initWithOverlay:overlay withCacheName:@"THE NEW BETTER CACHE!"];
}
return self.wmsOverlayView;
}
I also noticed that self.wmsOverlayView
was always nil
and always naming the cache CACHE!. So the wmsOverlayView
does get removed when the overlay is removed. But my timer kept going off. So I refactored my code to remove the timer and when I removed the WMSOverlay
, the cache was cleared out (got my cache:willEvictObject:
NSLog spammed).
So I believe the timer was keeping the WMSOverlayView
alive.
Moral of the story: don't place a timer in a MKOverlayView
.