2010-08-19 42 views
1

我一直在嘗試大約一天,以獲取表單元格以在加載新視圖時顯示活動指標。在didSelectRowAtIndexPath方法我想顯示的指標,而下面的運行作爲一個表視圖的活動指標加載另一個

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

該控制器然後運行

我已經搜查了相當密集的SQL查詢網頁和閱讀幾十個帖子,但似乎沒有幫助與我的具體問題。我知道我需要在另一個線程中運行指標,因爲推送和後續加載優先,但我不確定如何去做。我想過在控制器中運行SQL查詢之前,但這變得非常混亂。

奇怪的是,我一直在使用MBProgressHUD在這個相同的表視圖中顯示忙碌的遊標沒有問題。只有當我應用搜索,然後選擇導致此錯誤的結果之一:

bool _WebTryThreadLock(bool),0x1d79b0:試圖從主線程或Web線程以外的線程獲取Web鎖。這可能是從輔助線程調用UIKit的結果。現在崩潰...

該應用程序繼續在iPhone上,但崩潰的模擬器。

任何幫助將不勝感激。

回答

2

問題是,您在控制器中的任務持有UI代碼(但您可能已經知道!)。解決這個的廉價和簡單的方法是把一個微小的延遲上使用計時器開始你緩慢的任務(在這種情況下,您的SQL查詢):

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // instead of running the SQL here, run it in a little bit 
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(doSQL:) userInfo:nil repeats:NO]; 

    // Show the please wait stuff 
    [activityIndicator setHidden:NO]; 
} 

- (void)doSQL:(NSTimer *)timer { 
    // Do your sql here 
} 

解決此的另一種方式是將你的SQL進入一個單獨的線程:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // instead of running the SQL here, run it in a little bit 
    [self performSelectorInBackground:@selector(doSQL) withObject:nil]; 

    // Show the please wait stuff 
    [activityIndicator setHidden:NO]; 
} 

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

    // Do your sql here 

    // Tell the main thread 
    [self performSelectorOnMainThread:@selector(doneSQL) userInfo:nil waitUntilDone:YES]; 

    // Cleanup 
    [pool release]; 
} 

- (void)doneSQL { 
    // Update your UI here 
} 

希望幫助!

+0

謝謝。我來這裏刪除這個,因爲這是一個愚蠢的錯誤,我沒有打電話給'resignFirstResponder' 但是,謝謝你的答案。 – Matt 2010-08-19 14:14:19

相關問題