2010-09-28 30 views
0

我有一些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; 

有没有解决方法呢?我并不想成为分配/ INITING一个CLLocation每次的cellForRowAtIndexPath被称为..

+0

我在做CLLocations,所以我可以得到distanceFromLocation。基本上我正在制作一个UITableView,这些商店和他们距离我目前的位置的距离。 – 2010-09-28 03:42:55

回答

0

你得到的对象是一个NSString - 只需创建一个包含一个NSString,以及引用/中间数据的高德在必要的一类。然后使用观察者习惯用法,只是在字符串更改时更新单元格(设计它以便字符串取决于坐标)。你可以创建一个在初始化时使用一组参数的类(例如坐标),在初始化时创建一个NSString,然后在数据永不改变的情况下引用结果。它真的取决于你期望的数据会发生什么变化,以及以什么频率出现。

0

我不想被分配/ INITING一个 CLLocation每次 的cellForRowAtIndexPath被称为..

为什么不?你知道这是造成性能问题吗?你马上释放它们,所以它们没有占用额外的内存。 CLLocation看起来像一个非常轻量级的类,并且Objective-C运行时已经过很多优化,所以它们可能会很快地分配/ init。直到你看到滚动/ perf/memory的问题,我会选择适用的,并且易于维护。

过早的优化是所有罪恶的根源 - Donald Knuth

+0

对不起,忘了补充说它崩溃了。我通过将alloc init放入另一个方法来解决这个错误。 – 2010-09-28 05:34:29