2011-01-06 14 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]; 
} 

WHですエン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

してください[OK]が表示されていないと呼ばれていますか? uはuは唯一のバックグラウンド・プロセスを置くことができるスレッドでのごVIEWへのrelavent ...

任意のものを入れて傾ける

ので、あなたのコード内でuが

....スレッドで適切にテーブルのリロードメソッドを使用しますあなたが解析した後にテーブルをリロードしたい場合uあなたのコードでいくつかのフラグ値を使用することができ、Uロードテーブルを解析した後

-1

バックグラウンドスレッドからUI要素にアクセスすることはできません。あなたは確かにバックグラウンドスレッドでビューを作成することはできません。 「detachNewThreadSelector」メソッドの代わりに「performSelectorOnMainThread」メソッドを使用します。

すべて最高です。

3
  • クラッシュがある場合、バックトレースがあります。投稿してください。

  • メソッド名は小文字で始まり、キャメルケースで、アンダースコアは含まれません。これらの規則に従い、他のiOSプログラマーがあなたのコードを読みやすくなり、これらの規則を学ぶことで他のiOSプログラマーのコードを理解しやすくなります。

メインスレッドのメソッドをバックグラウンドスレッドから直接的または間接的に呼び出すことはできません。あなたのクラッシュとあなたのコードは、メインスレッド以外のスレッドと自由にやり取りしていることを示しています。

documentation on the use of threads in iOS applicationsは非常に豊富です。

0

-(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]; 
} 

そして、あなたはあなたのUIViewControllerはメインスレッドではありませんスレッドからロードするためにあなたの問題は来るべきUpdate_View方法に

2

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

線を移動しようとする変更CallParser方法に。あなたがビューをロードする前にデータを充電しようとするとちょっと。 これを手配するには、これを試すことができます 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]; 
}