2015-10-29

[iOS] GCD - dispatch


Queues Types:
- Serial Queues
Tasks in serial queues execute one at a time, each task starting only after the preceding task has finished.

- Concurrent Queues
Tasks in concurrent queues are guaranteed to start in the order they were added,
Items can finish in any order and you have no knowledge of the time it will take for the next block to start, nor the number of blocks that are running at any given time.
- Main Queues
Like any serial queue, tasks in this queue execute one at a time. However, it’s guaranteed that all tasks will execute on the main thread, which is the only thread allowed to update your UI. This queue is the one to use for sending messages to UIViews or posting notifications.

- Examples:
// background執行 dispatch_async(dispatch_get_global_queue(0, 0), ^{ // something });
// main thread執行 dispatch_async(dispatch_get_main_queue(), ^{ // something });
// 一次性執行: static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ // code to be executed once });
// delay 2 秒執行 double delayInSeconds = 2.0; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ // code to be executed on the main queue after delay });

// 自定義Queue (Serial Queue)
dispatch_queue_t urls_queue = dispatch_queue_create("blog.devtang.com", NULL); dispatch_async(urls_queue, ^{ // your code }); dispatch_release(urls_queue);

- dispatch_group:
GCD 还有一些高级用法,例如让后台 2 个线程并行执行,然后等 2 个线程都结束后,再汇总执行结果。这个可以用 dispatch_group, dispatch_group_async 和 dispatch_group_notify 来实现,示例如下:

dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{ // 並行執行的 Thread 1 }); dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{ // 並行執行的 Thread 2 }); dispatch_group_notify(group, dispatch_get_global_queue(0,0), ^{ // thread 1 & 2 完成後執行 });

[iOS] Auto Layout - NSLayoutConstraint

Object - C: 

NSMutableArray *myConstraints = [NSMutableArray array];
    
    NSDictionary *variableBindings = @{@"_btn_cancel" : _btn_cancel};
    NSDictionary *metrics = @{@"height": 100,
                              @"margin": 10};
    
   
    // ex1: 從水平方向佈局
    [myConstraints addObjectsFromArray:
     [NSLayoutConstraint constraintsWithVisualFormat:
                @"H:|-margin-[_btn_cancel(height)]-|"
                    options:NSLayoutFormatDirectionLeadingToTrailing
                    metrics:metrics
                     views:variableBindings]];
    
 //ex2: 從垂直方向佈局
 [myConstraints addObjectsFromArray:
    [NSLayoutConstraint constraintsWithVisualFormat:
        @"V:[ViewB(ViewA)]-|"
        options:NSLayoutFormatDirectionLeadingToTrailing
        metrics:nil
        views:NSDictionaryOfVariableBindings(ViewB,ViewA)]];
    


    [self.view addConstraints:myConstraints];



參數說明:

 options: 佈局時的順序,預設是由上而下(垂直)與由左而右(水平)

 metrics : 一般为nil ,参数类型为NSDictionary, 為要傳入的變數

views :
参数类型为NSDictionary, 為要傳入的Views



語法:


 " V: " " H: "
分別代表由垂直或是水平方向來佈局。

" | "
代表 Superview 的邊界。

" - "
代表預設距離,例如在中間加上數字 " -20- ",則代表限制 20 個單位距離。

*" [ ] "
代表物件本身,括號內包含物件的變數名稱與大小限制,可以使用關係運算子(<=、> == 等)。

* " @ "
優先權,1 1000 的整數,優先權較大的條件會優先被滿足,例如 [ViewB(>=100@1000)],物件 ViewB 不可以小於 100 個單位長度或寬度會最優先被考慮。