2010-09-28 31 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