2014年8月15日 星期五

async Thread programmatically using Swift and Object-C language

【說明】

async Thread,非同步執行緒,使用function找空閒的queue去執行次執行緒,使主執行緒可以順利的執行。


最常用在從網路上下載資料時,為了避免下載速度慢而影響了UI的回應,所以用次執行緒去做下載的動作,如此一來就可以保持主執行緒的執行順暢。

有點類似像是將工作移至背景執行的感覺。

此份筆記將紀錄如何實現async Thread。


【專案開發步驟】

建立專案:

使用Single View Application模板建立一個名為thread的專案,使用iPhone裝置。

設計使用者介面:

替View加入兩個Button,一個UIActivityIndicator,如下圖所示。

設計使用者介面
 修改ViewController:

建立中斷執行緒的function,如下所示,我們以執行執行緒的method(sleepForTimeInterval)來模擬主執行緒被中斷。

<Swift>
func doSomething1() {
    NSThread.sleepForTimeInterval(1.0)
}
func doSomething2() {
    NSThread.sleepForTimeInterval(2.0)
}
func doSomething3() {
    NSThread.sleepForTimeInterval(3.0)
}
func doSomething4() {
    NSThread.sleepForTimeInterval(4.0)
}
func doSomething5() {
    NSThread.sleepForTimeInterval(5.0)
}

<Object-C>
@interface ViewController ()
-(void)doSomething1;
-(void)doSomething2;
-(void)doSomething3;
-(void)doSomething4;
-(void)doSomething5;
@end

@implementation ViewController
-(void)doSomething1 {
    [NSThread sleepForTimeInterval:1.0];
}
-(void)doSomething2 {
    [NSThread sleepForTimeInterval:2.0];
}
-(void)doSomething3 {
    [NSThread sleepForTimeInterval:3.0];
}
-(void)doSomething4 {
    [NSThread sleepForTimeInterval:4.0];
}
-(void)doSomething5 {
    [NSThread sleepForTimeInterval:5.0];
}
@end

撰寫當按鈕按下後的function,如下所示。

<Swift>
@IBAction func click(sender: UIButton) {
    sender.enabled = false
    activityIndicator.startAnimating()
        
    dispatch_async(dispatch_get_global_queue(0, 0), {
        self.doSomething1()
        self.doSomething2()
        self.doSomething3()
        self.doSomething4()
        self.doSomething5()
            
        dispatch_async(dispatch_get_main_queue(), {
            sender.enabled = true
            self.activityIndicator.stopAnimating()
        })
    })
}

<Object-C>
- (IBAction)click:(UIButton *)sender {
    sender.enabled=NO;
    [self.activityIndicator startAnimating];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [self doSomething1];
        [self doSomething2];
        [self doSomething3];
        [self doSomething4];
        [self doSomething5];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            sender.enabled=YES;
            [self.activityIndicator stopAnimating];
        });
    });
}
當按鈕按下後將按鈕關閉。讓activityIndicator開始旋轉。使用function despatch_async找空閒的queue執行次執行緒。當doSomething做完後使用function回到主執行緒去執行將按鈕開啟以及停止activityIndicator。


【執行結果】

    



【專案範例】

沒有留言:

張貼留言