나열된 "집"위치 또는 현재 위치 (CLLocationManager 사용)를 기반으로하는 상점을 나열하는 UIPickerView 개체가 있습니다. 후자가 구현되면 가장 가까운 상점을 얻기 위해 내 서버에 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"];
}
}
한 생각이 두 번째, 더 정확한 결과를 제공하는 것을 나는 이미 발표 한 수 뭔가를 발표 한 것이 었습니다.
- (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 방법은 다음과 같습니다 : 내 CLLocationManager 코드는
- (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;
}
}
임 호기심 내가 피커 그냥 충돌하기 전에 업데이트되는 것을 볼 수 있습니다 이유에. 이것은 내가 길을 갈 때 발생하기 때문에 정확성이 의심스럽고 위치 관리자가 두 번째 결과를 보내서 충돌을 일으켰습니다. 이견있는 사람?
모든 위치 변수가 속성이라고 가정합니다. 오류가 발생하는 동안 모두 풀어주는 이유가 있습니까? 당신은 dealloc 때까지 해제하지 않아야합니다. 위치 업데이트를 중지 할 수는 있지만보기가 완료 될 때까지 변수를 유지합니다. –
안녕 빌, 응답 주셔서 감사합니다 - 나는 같은 클래스에서, 나는 사용자가 "현재 위치"버튼을 다시 눌러야 새로운 인스턴스를 생성했기 때문에 그들을 릴리스했습니다. 그 이후로 아무 것도 제대로 캡슐화되지 않았고 이후 각 GPS 호출을 자체 클래스에 넣었습니다. Anyhoo - 이것이 사물이 공개되는 이유입니다. 귀하의 의견을 보내 주셔서 감사합니다! –