2013-07-31 2 views
2

작동중인 테이블보기에 4 개 도시 문자열을로드하고 있는데 셀 중 하나를 선택하고 다른 테이블로 이동하면 탐색 속도가 너무 느려집니다. 다른 링크가있는 다른 테이블에서 아래의 동일한 코드를 사용하고 있습니다. 다른 시각을보기 위해 왜 오랜 시간 (~ 4 - 6 초)이 걸릴지 말해 주시겠습니까?JSON을 사용하여 UITableView를 탐색 할 때 문제가 발생합니다.

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

NSURL * url = [NSURL URLWithString:@"http://kalkatawi.com/jsonTest.php"]; 

NSData * data = [NSData dataWithContentsOfURL:url]; 

NSError *e = nil; 

jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&e]; 

jsonArray1 = [[NSMutableArray alloc] init]; 

for(int i=0;i<[jsonArray count];i++) 
{     
    NSString * city = [[jsonArray objectAtIndex:i] objectForKey:@"city"]; 

    [jsonArray1 addObject:city]; 
} 

-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

NSString *tempString = [jsonArray1 objectAtIndex:indexPath.row]; 
cell.textLabel.text = tempString; 
return cell; 
} 

- 당신이 동기 다운로드 및 메인 스레드를 차단하고 어쩌면 때문에

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

SeconfViewController *second = [[SeconfViewController alloc] initWithNibName:@"SeconfViewController" bundle:nil]; 

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; 

NSString *cellText = selectedCell.textLabel.text; 

NSString *edit = [NSString stringWithFormat:@"http://kalkatawi.com/jsonTest.php?d=1&il=%@", cellText]; 

second.str2 = edit; 

[self.navigationController pushViewController:second animated:YES]; 

} 
+0

주 스레드에서 동기식 네트워크 호출을하는 것과 같은 소리가납니다. 비동기 네트워크 호출을 만드는 방법을 찾으십시오. 수많은 예제가 있습니다. –

답변

0

어쩌면 그 이유가 4-6 초 동안 앱 동결 , json 비동기를 다운로드하십시오.

- (void)viewDidLoad 
{ 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    NSData *response = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://kalkatawi.com/jsonTest.php"]]; 
    NSError *parseError = nil; 
    jsonArray = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:&parseError]; 
    jsonArray1 = [[NSMutableArray alloc] init] 
     for(int i=0;i<[jsonArray count];i++) 
     {     
      NSString * city = [[jsonArray objectAtIndex:i] objectForKey:@"city"]; 

      [jsonArray1 addObject:city]; 
     } 
    } 
    dispatch_sync(dispatch_get_main_queue(), ^{ 
      [self.myTableView reloadData]; 
     }); 
}); 
} 
+0

NSData의'initWithContentsOfURL :'은 _remote_ 리소스에 액세스하는 데 사용해서는 안됩니다. 이 사실에 대한 문서는 꽤 조용하지만, 공식 Apple 개발자 포럼에는 Apple 기술자에게 'initWithContentsOfURL :'패밀리가 _file access_에 대해서만 사용해야한다는 내용이 상당히 많이 있습니다. 네트워크를 통해 원격 자원에 액세스하려면'NSURLConnection'을 사용하십시오. – CouchDeveloper

+0

@CouchDeveloper @CarlosVela 이제는 이전보다 더 빠르게 작동합니다. 나는 그것을 언급했다; 먼저'UITableView'를 보여주고 데이터를로드하면 어쨌든이 문제를 개선 할 수 있습니까? –

+0

@LuaiKalkatawi 활동 표시기를 사용하여 웹 서비스에서 데이터가 다운로드되고 있음을 사용자에게 알리십시오. –

1

서버의 데이터를 동 기적으로로드하기 때문에 다른 화면에서 탐색하는 데 더 많은 시간이 걸립니다. iOS에서 모든 UI는 기본 스레드에서 수행되며 기본 스레드에서 데이터 호출을 수행하여 차단합니다. 내가 이것을 처리하는 가장 좋은 방법은 GCD (그랜드 센트럴 디스패치)를 사용하는 것입니다. iOS의 API로 번거롭지 않게 스레드를 생성 할 수 있습니다. 백그라운드 스레드의 서버에서 데이터를로드하라는 호출을 원한다는 것을 알면됩니다. 그렇게하면보기가 즉시 탐색해야합니다. 데이터가 나오는 동안 활동 표시기를 사용할 수 있습니다.

dispatch_async(dataQueue, ^{ 

     // Load all your data here 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      // Update the UI 

     }); 

    });