CLLocation 内存问题
我的 CLLocation 存在一些内存问题。
CLLocation *annotation = [[CLLocation alloc] initWithLatitude:[[tempDict objectForKey:@"lat"] doubleValue] longitude:[[tempDict objectForKey:@"lon"]doubleValue]];
CLLocation *item2 = [[CLLocation alloc] initWithLatitude:[newLatString doubleValue] longitude:[newLongString doubleValue]];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%.1f km",[item2 distanceFromLocation:annotation]/1000];
[annotation release];
[item2 release];
所以我尝试这样做,但我意识到你无法设置注释的坐标。
CLLocationCoordinate2D tempCoordinate = annotation.coordinate;
tempCoordinate.latitude = [[tempDict objectForKey:@"lat"] doubleValue];
tempCoordinate.longitude = [[tempDict objectForKey:@"lon"] doubleValue];
annotation.coordinate = tempCoordinate;
有解决方法吗?我不想每次调用 cellForRowAtIndexPath 时都分配/初始化 CLLocation。
I've some memory issues with CLLocation.
CLLocation *annotation = [[CLLocation alloc] initWithLatitude:[[tempDict objectForKey:@"lat"] doubleValue] longitude:[[tempDict objectForKey:@"lon"]doubleValue]];
CLLocation *item2 = [[CLLocation alloc] initWithLatitude:[newLatString doubleValue] longitude:[newLongString doubleValue]];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%.1f km",[item2 distanceFromLocation:annotation]/1000];
[annotation release];
[item2 release];
So I tried to do this, but I realised that you can't set the annotation's coordinate.
CLLocationCoordinate2D tempCoordinate = annotation.coordinate;
tempCoordinate.latitude = [[tempDict objectForKey:@"lat"] doubleValue];
tempCoordinate.longitude = [[tempDict objectForKey:@"lon"] doubleValue];
annotation.coordinate = tempCoordinate;
Is there a workaround this? I don't want to be alloc/initing a CLLocation everytime cellForRowAtIndexPath is called..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的结果对象是一个 NSString - 只需创建一个包含 NSString 的类,以及必要时中间数据的引用/ivars。然后使用观察者习惯用法,只需在字符串更改时更新单元格(设计它以便字符串取决于坐标)。您可能可以创建一个类,它在初始化时接受一组参数(例如坐标),在初始化期间创建一个 NSString,然后在数据从未更改的情况下引用结果。这实际上取决于您期望哪些数据会发生变异以及以什么频率发生变异。
your resultant object is an NSString - just create a class which contains an NSString, as well as references/ivars of the intermediate data where necessary. then using an observer idiom, just update the cells when the string changes (design it so the string depends on the coordinates). you could probably make a class which takes a set of arguments at initialization (e.g. coordinates), creates an NSString during initialization, and then refer to the result if your data never changes. it really depends on what data you expect will mutate, and at what frequency.
为什么不呢?您知道这会导致性能问题吗?您立即释放它们,因此它们不会占用额外的内存。 CLLocation 看起来像一个非常轻量级的类,并且 Objective-C 运行时经过了深度优化,因此它们可能会很快分配/初始化。在您看到滚动/性能/内存问题之前,我会选择有效且易于维护的方法。
Why not? Do you know it's causing performance problems? You're releasing them right away, so they aren't taking up extra memory. CLLocation looks like a pretty lightweight class, and the Objective-C runtime is heavily optimized, so they probably alloc / init pretty quickly. Until you see scrolling / perf / memory issue, I would go with what works and is easy to maintain.