2013-05-04 71 views
1

我是新來的iOS開發線程,我有問題在這裏考慮下面的代碼的iOS在一個線程中運行多個操作

[activityIndicator startAnimating]; 
    [self startAnewOperationInSepThread]; 
    [self startAnotherOperationInSepThread]; 

這裏我的理解是指在執行第一線的時候,活動的指標將被啓動在主線程中。所以現在主線程負責運行活動指示器。隨後的操作將如何被調用。

+0

你想從其他線程訪問activityIndi​​cator嗎? – 2013-05-04 05:57:41

+0

上面的代碼工作正常,活動指標是動畫和其他後續操作被調用,但我不清楚如何調用這兩種方法,因爲主線程現在正在處理活動指標,沒有辦法調用其他兩種方法。 – Karthick 2013-05-04 06:19:02

回答

0

號當第一線執行活動的指標未啓動。只要主循環調用的主線程中的方法結束,它就會啓動。當你的方法在主線程中運行時,UI將不會改變。

1

使用performSelectorInBackground

[self performSelectorInBackground:@selector(yourmethod:) withObject:nil]; 
0

是的,你調用[activityIndi​​cator startAnimating];從主線程,這只是一個調用來啓動動畫。實際的動畫是在一個單獨的線程上完成的。

即使活動指示器仍然是動畫,這並不意味着您無法對主線程執行任何操作。因爲他的職責(稱爲startAnimating方法)已經結束。

在activityIndi​​cator上調用startAnimating後,它跳到下一行並執行[self myMethod1];然後[self myMethod2];爲下面的代碼。

不要讓他們複雜的,這就是這個簡單的

[activityIndicator startAnimating]; 
    [self myMethod1]; 
    [self myMethod2]; 
0

你必須選擇它

1>您可以運行在不同的兩種兩種方法 - 不同的線程喜歡

[NSThread detachNewThreadSelector:@selector(startAnewOperationInSepThread) toTarget:self withObject:nil]; 

[NSThread detachNewThreadSelector:@selector(startAnotherOperationInSepThread) toTarget:self withObject:nil]; 

OR

2>如果你想上線後,已完成其他將開始然後首先調用startAnewOperationInSepThread方法,它的定義調用內部startAnotherOperationInSepThread喜歡

-(void)startAnotherOperationInSepThread 
{ 
    [self startAnotherOperationInSepThread]; 
} 

請讓我知道如果仍然ü面臨任何問題。謝謝

相關問題