2011-01-06 4 views
8

NSThread를 사용할 때 콘솔에 다음과 같은 오류 메시지가 표시됩니다. "주 스레드 나 웹 스레드가 아닌 다른 스레드에서 웹 잠금을 얻으려고했습니다.이 오류는 보조 스레드에서 UIKit을 호출 한 결과 일 수 있습니다. .. "스레드를 사용할 때 오류를 해결하는 방법은 무엇입니까?

여기 내 샘플 코드 여기

- (void)viewDidLoad { 

    appDeleg = (NewAshley_MedisonAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    [[self tblView1] setRowHeight:80.0]; 
    [super viewDidLoad]; 
    self.title = @"Under Ground"; 


    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
    [NSThread detachNewThreadSelector:@selector(CallParser) toTarget:self withObject:nil]; 

} 

-(void)CallParser { 


    Parsing *parsing = [[Parsing alloc] init]; 
    [parsing DownloadAndParseUnderground]; 

    [parsing release]; 
    [self Update_View]; 
    //[myIndicator stopAnimating]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 




} 

제출해야"DownloadAndParseUnderground "는 RSS 피드에서 데이터를 downloding의 방법

-(void) Update_View{ 


    [self.tblView1 reloadData]; 
} 

어이다 EN Update_View 방법은있는 tableView 당신이 방법을 구문 분석 스레드를 필요로하는 이유 적절한 설명 데이터를 다시로드하고 cellForRowAtIndexPath에서 오류를 생성하고 정의 셀

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


    static NSString *CellIdentifier = @"Cell"; 

    CustomTableviewCell *cell = (CustomTableviewCell *) [tblView1 dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     [[NSBundle mainBundle] loadNibNamed:@"customCell" 
             owner:self 
            options:nil]; 

     cell = objCustCell; 
     objCustCell = nil; 

    } 
+0

비동기 로딩에 "PerformSelectorOnMainThread"에 코드 (아래 주석) 2.Change 한 PARAM

-(void)pushViewController:(UIViewController*)theViewController{ [self.navigationController pushViewController:theViewController animated:YES];} 

와의 ViewController을로드하는 방법을 추가, 왜? – BoltClock

+1

답변을 읽는 것은 두 가지 모두가 잘못 되었기 때문입니다 (또는 파싱 할 수없는 것 - 무엇이 '그 모든 것 위에 올랐습니까 ??!?). – bbum

답변

0

하십시오 확인을 표시하지이라고? 코드에서 유

이 유는 백그라운드 프로세스를 넣을 수 있습니다 ... U이 스레드에서보기에 relavent 어떤 일을 넣어 캔트

때문에 .... 제대로 스레드에서 테이블 새로 고침 방법을 사용 구문 분석 할 때 ... 구문 분석 후 테이블을 다시로드하려는 경우 코드 및 U 구문 분석 후 일부 플래그 값을 사용할 수 있습니다.

-1

백그라운드 스레드에서 UI 요소에 액세스 할 수 없습니다. 확실히 백그라운드 스레드에서 뷰를 작성할 수는 없습니다. "detachNewThreadSelector"메서드 대신 "performSelectorOnMainThread"메서드를 사용하십시오.

모두 최고입니다.

3
  • 크래시가있는 경우 백 트레이스가 있습니다. 게시 해주세요.

  • 메소드 이름은 소문자로 시작하고, camelCased이며, 밑줄을 포함하지 않습니다. 이러한 규칙에 따라 다른 iOS 프로그래머가 코드를 더 쉽게 읽을 수 있으며 이러한 규칙을 학습하면 다른 iOS 프로그래머 코드를 더 쉽게 이해할 수 있습니다.

백그라운드 스레드에서 주 스레드의 메소드를 직접 또는 간접적으로 호출 할 수 없습니다. 당신의 크래쉬와 코드는 모두 메인 스레드가 아닌 메인 스레드와 자유롭게 상호 작용한다는 것을 나타냅니다.

documentation on the use of threads in iOS applications은 매우 광범위합니다.

0

한번에 변경 CallParser 방법

-(void)CallParser { 

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

    Parsing *parsing = [[Parsing alloc] init]; 
    [parsing DownloadAndParseUnderground]; 

    [self performSelectorOnMainThread:@selector(Update_View) 
          withObject:nil 
         waitUntilDone:NO]; 
    [parsing release]; 
    [pool release]; 
} 

및 이동

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

라인 Update_View 방법에

2

메인 스레드 아니다 스레드에서의 UIViewController를로드하기 때문에 귀하의 문제가 와야한다 . 보기를로드하기 전에 데이터를 충전하려고 할 때. 이것을 조정하려고하면 1을 시도 할 수 있습니다.두 답변은을 downvoted했다

-(void)asyncLoadMyViewController 
{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    MyViewController *myVC = [[myVC alloc] initWithNibName:@"myVC" bundle:nil ]; 
    [self performSelectorOnMainThread:@selector(pushViewController:) withObject:myVC waitUntilDone:YES]; 
    //  [self.navigationController pushViewController:wnVC animated:YES]; 
    [wnVC release]; 
    [pool release]; 
} 
관련 문제