iPhoneアプリ作成(17) 意味不明なクラッシュ Tried to obtain the web lock ~
Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now…
tableViewを別スレッドからreloadDataで再表示しようとしたが、上記のエラーが表示されクラッシュしてしまう。
ダメもとでググってみたら、下記のサイトを見つける。
意味不明なエラーに遭遇しているのは、私だけでは無いんですね。ほんと、助かりました。感謝感謝です。
http://blog.ps-ax.info/2009/09/uikit-isnt-thread-safe/
◆修正内容
<変更前>
(メインスレッドからsyncProc:をサブスレッドとして起動させる) [NSThread detachNewThreadSelector:@selector(syncProc:) toTarget:self withObject:indicator]; -(void)syncProc:(id)anArgument{ id pool = [[NSAutoreleasePool alloc]init]; (サブスレッド処理 :メインスレッドに置けないインジケータなんかの処理を書く) (サブスレッドでreloadDataを実行させると、意味不明なエラーに遭遇する場合がある。 必ず発生しないのでやっかい) [self.tableView reloadData]; [pool drain]; }
<変更後>
(メインスレッド) [NSThread detachNewThreadSelector:@selector(syncProc:) toTarget:self withObject:indicator]; -(void)syncProc:(id)anArgument{ id pool = [[NSAutoreleasePool alloc]init]; (サブスレッド処理 :メインスレッドに置けない処理を書いておく) (メインスレッドとしてreload:メソッドを起動させてやる) [self performSelectorOnMainThread:@selector(reload:) withObject:self waitUntilDone:TRUE]; [pool drain]; } -(void)reload:(id)anArgument{ id pool = [[NSAutoreleasePool alloc]init]; (reloadDataはメインスレッドして実行され、tableViewはめでたく再表示される) [self.tableView reloadData]; [pool drain]; }