CLLocation 内存问题

发布于 2024-09-24 20:29:41 字数 873 浏览 7 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

避讳 2024-10-01 20:29:41

你的结果对象是一个 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.

猫卆 2024-10-01 20:29:41

我不想分配/初始化
CL每次位置
cellForRowAtIndexPath 被调用..

为什么不呢?您知道这会导致性能问题吗?您立即释放它们,因此它们不会占用额外的内存。 CLLocation 看起来像一个非常轻量级的类,并且 Objective-C 运行时经过了深度优化,因此它们可能会很快分配/初始化。在您看到滚动/性能/内存问题之前,我会选择有效且易于维护的方法。

过早的优化是万恶之源 - Donald Knuth

I don't want to be alloc/initing a
CLLocation everytime
cellForRowAtIndexPath is called..

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.

Premature optimization is the root of all evil - Donald Knuth

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文