Cocoapods第三方套件管理 - iOS開發必吃的可可豆

  • 0

安裝Cocoapods

在終端機下,輸入:

$ sudo gem install cocoapods


建立podfile

在終端機下,cd切換到專案的資料夾,輸入:

$ vi Podfile

目的要建立一個檔案讓cocoapods讀取你要管理的第三方套件


建入文字輸入介面,然後輸入以下資訊

platform:ios, '7.0'
pod 'AFNetworking', '~> 2.4.1'
pod 'ECSlidingViewController', '~> 2.0.3’


pod後面接著的是你要的套件名稱,後面是版本號,若不加,預設是下載最新的版本。
輸入完成後,按Esc到指令模式,輸入:wq,存檔離開。


下載/更新套件

$ pod update
這時cocoapods會根據你設定的iOS版本號和套件做相依性檢查。

使用套件

完成後,套件就會下載到你的專案資料夾,此時的資料夾會多幾個資料夾和檔案。
裝了cocoapods之後都必須要用.xcworkspace開啟專案。


往後如果要新增或更新套件,就再去修改cocoapods,加入你要下載的套件名稱即可。

使用套件的方法也很簡單,直接在程式裡import “xxx.h”就可以囉!大功告成





參考資料

http://cocoapods.org/

GCD - dispatch_queue的基本用法

  • 0
Grand Central Dispatch(GCD),是Apple用來處理多執行緒的一個機制,執行緒交由OS管理,效能也更好。

Dispatch Queues有以下3種類型:

1.Serial Queue:串列執行block,會依照放入的順序執行。
2.Concurrent Queue:可以同時執行多個block,但是執行完成的順序是隨機的。
3.Main dispatch queue:在應用程式的main thread中執行block。


下面是幾個簡單的使用方式:

1.Serial Queue

建立Serial Queue的方法很簡單,首先先宣告一個dispatch_queue_create建立一個dispatch_queue_t,然後執行dispatch_async的block。

範例裡分別執行2個Serial Queue,尤於是Serial Queue一次只執行一次的特性的,所以會先執行Run1,結束後再執行Run2。

dispatch_queue_t createQueue = dispatch_queue_create("SerialQueue", nil);
    dispatch_async(createQueue, ^(){
        [self run1];
    });
    dispatch_async(createQueue, ^(){
        [self run2];
    });

- (void)run1{
    for (int i=0; i<5 br="" i="">     
[NSThread sleepForTimeInterval:1];
        NSLog(@"Run1:%d",i);
    }
}
- (void)run2{
    for (int i=0; i<5 br="" i="">     
[NSThread sleepForTimeInterval:0.3f];
        NSLog(@"*Run2:%d",i);
    }
}
執行結果

Run1:0
Run1:1
Run1:2
Run1:3
Run1:4
*Run2:0
*Run2:1
*Run2:2
*Run2:3
*Run2:4
有時,當我們需要在背景用執行緒讀取資料,完成後再更新UI,這時可以用以下的寫法:

    dispatch_queue_t createQueue = dispatch_queue_create("SerialQueue", nil);
    dispatch_async(createQueue, ^(){
        [self run1];
        dispatch_async(dispatch_get_main_queue(), ^(){
            [self run2];
        });
    });

執行結果跟上面的例子一樣,但不同的是,當run1執行完成後,run2是交由dispatch_get_main_queue(),也就是Main Thread來完成,所以當有需要更新UI的地方,可以採取這樣的寫法。


2.Concurrent Queue

建立Cocurrent Queue的方法如下,首先建立dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),DISPATCH_QUEUE_PRIORITY_DEFAULT的意思是交由OS來管理優先順序,開發者可以根據需求調整執行的優先權,系統提供4種:
#define DISPATCH_QUEUE_PRIORITY_HIGH 2
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0
#define DISPATCH_QUEUE_PRIORITY_LOW (-2)
#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^(){
        [self run1];
    });
    dispatch_async(queue, ^(){
        [self run2];
    });
執行結果
*Run2:0
*Run2:1
*Run2:2
Run1:0
*Run2:3
*Run2:4
Run1:1
Run1:2
Run1:3
Run1:4
可以看到,執行的結果完全是隨機的,也就是說,系統是同時在背景執行2個block。


3.Main dispatch queue

尤於是在主執行緒上執行,所以執行的時候,其它正在執行緒上跑的block都會排隊執行。

dispatch_queue_t mainQueue = dispatch_get_main_queue();
    dispatch_async(mainQueue, ^(){
        [self run1];
        dispatch_async(dispatch_get_main_queue(), ^(){
            [self run2];
        });
    });

執行結果
Run1:0
Run1:1
Run1:2
Run1:3
Run1:4
*Run2:0
*Run2:1
*Run2:2
*Run2:3
*Run2:4 

後續還有Group of Queue,待我研究後補上。


參考資料
GCD介绍(一): 基本概念和Dispatch Queue
Austin's study note - 多工筆記 (GCD)