1

我有一個UIPickerView對象,該對象基於列出的「主」位置或當前位置(使用CLLocationManager)列出商店。如果後者被實現,我向我的服務器做一個NSMutableURLRequest以獲得最近的商店,然後用接收的列表更新一個UIPickerView。CLLocationManager是否會導致此崩潰?

有時候(當我在「家」的位置時,從來不會奇怪),我會使用當前位置,我會看到選取器更新列表,然後應用程序立即崩潰。

我選擇器的代碼很簡單:

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 
return 1; 
} 

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 
    if (isHome) { 
     return [storesData count]; 
    } else { 
     return [storesDataLoc count]; 
    } 
} 

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 
    if (isHome) { 
     return [[storesData objectAtIndex:row] objectForKey:@"STName"]; 
    } else { 
     return [[storesDataLoc objectAtIndex:row] objectForKey:@"STName"]; 
    } 
} 

一個想到的是,它提供了一個第二,更準確的讀數和我釋放的東西,我可能已經釋放。我CLLocationManager代碼:

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { 
if (error.code == kCLErrorLocationUnknown) { 
    NSLog(@"Currently unable to retrieve location"); 
} else if (error.code == kCLErrorNetwork) { 
    NSLog(@"Network used to retrieve location is unavailable"); 
} else if (error.code == kCLErrorDenied) { 
    NSLog(@"Permission to retrieve location is denied"); 
    [locMan stopUpdatingLocation]; 
    [locMan release]; 
    locMan = nil; 

    // revert segmented controller to Home position 
    storeSource.selectedSegmentIndex = 0; 
} 
if(loadstoresconnection!=nil){ 
    [loadstoresconnection release]; 
} 
networkView.hidden = TRUE; 
isHome = TRUE; 
} 

- (void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
     fromLocation:(CLLocation *)oldLocation { 

if (newLocation.horizontalAccuracy >= 0) { 
    networkView.hidden = TRUE; 
    if (newLocation.horizontalAccuracy < 200) { 
     [locMan stopUpdatingLocation]; 
     [locMan release]; 
     locMan = nil; 
    } 

    //call for store list from this location 
    NSString *myString = [NSString stringWithFormat:@"http://mywebsite.com/?lat=%f&lng=%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude]; 
    NSURL *myURL = [[NSURL alloc] initWithString:[myString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60]; 
    loadstoresconnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
} 
} 

和配屬NSMutableURLRequest方法是:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
[responseData release]; 
[connection release]; 
networkView.hidden = TRUE; 
isHome = YES; 
// [textView setString:@"Unable to fetch data"]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
int i; 
NSArray *querydata; 

networkView.hidden = TRUE; 
[loadstoresconnection release]; 
if (storesDataLoc!=nil) { 
    [storesDataLoc release]; 
    storesDataLoc = nil; 
} 
storesDataLoc = [[NSMutableArray alloc] init]; 
NSString *txt = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease]; 
[responseData release]; 
// put data into variables. 
querydata = [txt componentsSeparatedByString:@"<-sl->"];//break up data into data sections: 0 - number of deptsections and names, 1 - list of objects 
NSArray *allstoreinfo; 
for (i=0; i<[querydata count]; i++) { 
    allstoreinfo = [[querydata objectAtIndex:i] componentsSeparatedByString:@"<-as->"]; 
    [storesDataLoc addObject:[[[NSMutableDictionary alloc] initWithObjectsAndKeys:[allstoreinfo objectAtIndex:0],@"STid",[allstoreinfo objectAtIndex:1],@"STName",[allstoreinfo objectAtIndex:2],@"STAddr",nil] autorelease]]; 
} 
if ([querydata count]>0) { 
    [pickerView reload]; 
    [pickerView selectRow:0 inComponent:0 animated:NO]; 
    isHome = NO; 
}  

}

林好奇,爲什麼我可以看到選擇器就在崩潰之前被更新。因爲這是在路上發生的,所以我懷疑這是一個準確的事情,而位置管理器正在發送第二個結果,導致崩潰。有什麼想法嗎?

+0

我假設你所有的位置變量都是屬性。是否有理由在錯誤期間釋放它們?你不應該釋放他們,直到dealloc。你可以stopUpdatingLocation很好,但我會保留你的變量,直到你完成視圖。 – 2012-01-30 15:22:25

+0

嗨,比爾,謝謝你的迴應 - 我正在發佈他們,因爲在同一班,我正在創建新的實例,如果用戶再次按下「當前位置」按鈕。我意識到自從沒有任何東西被恰當地封裝,並且從此將每個GPS呼叫放入其自己的類中。 Anyhoo--這就是爲什麼事情正在被釋放。感謝您的輸入! – 2012-03-05 00:06:07

回答

0

看來我試圖做太多的快捷方式。當用戶點擊位置按鈕時,它會打開LocationManager,每個結果都會爲我的服務器建立一個NSURLConnection。我被帶到一個班級來處理所有這些,但隨着位置管理器返回的多個結果,NSURLConnections似乎不受控制。從那以後,我把每個位置管理員的成績都放在了自己的班級裏,而且所有的工作都很順利。所以,選擇器沒有問題 - 主要是位置管理器和nsurlconnections的內存問題。

+0

如果這回答你的問題,那麼你應該把你的答案標記爲答案。 – ThomasW 2012-03-05 00:16:08

+0

忘了 - 謝謝! – 2012-03-06 05:50:08