2011-02-10 3 views
0

내 iPhone 앱에는 UITabBarController을 사용하여 세 개의 탭이 있습니다. 첫 번째 탭 (앱 실행시로드 됨)은로드하기 위해 로컬 데이터를 사용하며 매우 빠릅니다.탭 전환 (초기) 매우 느림 - 기능을 viewDidLoad 밖으로 이동 하시겠습니까?

웹에서 XML 파일을 다운로드하여 구문 분석 한 다음 UITableView의 모든 데이터를 표시하는 두 번째 탭은 느린 연결 (EDGE, 3G)을로드하는 데 오랜 시간이 걸립니다. 그리고 내 파서를 viewDidLoad으로 호출하면 모든 것이 완료 될 때까지 앱이 두 번째 탭으로 전환되지 않습니다. 이는 탭을로드하는 데 시간이 걸리고 앱이 잠긴 것처럼 보입니다.

필자는 사용자가 해당 탭으로 전환하도록하고 비어있는 경우에도보기로드를 즉시 수행 한 다음 데이터를 다운로드/구문 분석/표시하도록 할 수 있습니다. 네트워크 활동 회 전자가 회전 중이므로 최소한 사용자는 상황이 무엇인지 알 수 있습니다.

은 여기 내 viewDidLoad 현재 : 나는 백그라운드에서 스레드를 실행하는 방법을 보여줍니다있는 this article을 찾았지만, 내가 그 코드를 구현하는 시도로 데이터를 다시로드 백그라운드 스레드를 가져올 수 없습니다

// Load in the latest stories when the app is launched. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSLog(@"Loading news view"); 

    articles = [[NSMutableArray alloc] init]; 

    NSURL *url = [NSURL URLWithString:@"http://example.com/mobile-app/latest-news.xml"]; 
    NSLog(@"About to parse URL: %@", url); 
    parser = [[NSXMLParser alloc] initWithContentsOfURL:url]; 
    parser.delegate = self; 
    [parser parse]; 
} 

내 UITableView - 코드가 호출되었지만 구문 분석 된 기사가 내 테이블 뷰에 다시로드되는지 확인하려면 어떻게해야합니까?

+0

또한 및 나는 다른 탭으로 되돌아 간다. 모든 것이 빠르다 (왜냐하면 viewDidLoad는 뷰의 첫 번째로드에서만 호출되고 낮은 메모리 조건 이후에 ...). – geerlingguy

+0

찾은이 - http://stackoverflow.com/questions/2987672/with-viewdidload-my-viewcontroller-take-much-time-to-appear - 그럼, 질문, 질문 : 어떻게 파서를 오프로드합니까 배경 스레드? – geerlingguy

답변

3

2 차 스레드에서 데이터를로드 한 후 UITableView를 다시로드해야하므로 모든 사항이 수정되었습니다.

가 여기 내 보조 스레드 +의 viewDidLoad 기능의 마지막 코드입니다 :

-(void)initilizeNewsViewWithData { 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

    // Start the network activity spinner in the top status bar. 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 

    articles = [[NSMutableArray alloc] init]; 
    NSURL *url = [NSURL URLWithString:@"http://example.com/mobile-app/latest-news.xml"]; 
    parser = [[NSXMLParser alloc] initWithContentsOfURL:url]; 
    parser.delegate = self; 
    [parser parse]; 
    [tblLatestNews reloadData]; // Need to refresh the table after we fill up the array again. 

    [pool release]; 
} 

// Load in the latest news stories in a secondary thread. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [self performSelectorInBackground:@selector(initilizeNewsViewWithData) withObject:nil]; 
} 

나는이 대답에서 테이블 뷰를 새로 고칠 생각 나게했다 : 데이터가 처음로드 한 후, UITableView not displaying parsed data

+2

요즘 내 질문에 대부분 답하고있는 것처럼 보입니다.이게 뭐야? – geerlingguy