我是新来的Objective-C,所以请原谅我的无知:删除MKAnnotations
我已经宣布了一种方法,一类WGMap : MKMapView
像这样:
- (void)cleanupMap { NSLog(@"Cleaning up the map!"); NSSet* visible = [self annotationsInMapRect:[self visibleMapRect]]; NSArray* all = [self annotations]; NSMutableArray* discard = [NSMutableArray array]; for (id<MKAnnotation> cur in all) { if (![visible containsObject:cur]) [discard addObject:cur]; } [self removeAnnotations:discard]; }
目的是清理当前不在屏幕上的MKAnnotations以节省内存。我使用GCD调度计时器定期调用此方法。
我有另一种并行运行的方法,偶尔会将MKAnnotations添加到MKMapView。我注意到我偶尔会得到以下错误: **** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x17025ed20> was mutated while being enumerated.'
有时,我会得到一个类似的数组超出范围的错误。
我该如何着手预防呢?我试过让我的班级(从MKMapView派生)NSArray* annotations
属性atomic
而不是默认的nonatomic
,但它没有解决问题。
如果viewForAnnotation方法已实现并正确使用视图出队/重用机制,则不需要自己执行此操作。你怎么知道你有“高内存使用率”问题? – Anna 2014-08-28 10:49:58