iOS block 详解

//
//  ViewController.m
//  test_addEntryDictionary_01
//
//  Created by jeffasd on 16/6/26.
//  Copyright © 2016年 jeffasd. All rights reserved.
//

#import "ViewController.h"

typedef int(^Myblock)(int, int);

typedef int(^TyBlock)(int, int);

@interface ViewController ()

@property (nonatomic, copy) TyBlock tyBlock;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self testAsyncBLock1:^int(int a, int b) {
//       
//        NSLog(@" blcok is %@", [NSThread currentThread]);
//        
//        NSLog(@"asss");
//        
//        return a + b;
//    }];
    
    [self testSyncBlock:^int(int a, int b) {
        
        NSLog(@" blcok is %@", [NSThread currentThread]);
        
        NSLog(@"asss");
        
        return a + b;
    }];
    
    NSLog(@"test who is first");
    
    NSLog(@"--- thread is %@", [NSThread currentThread]);
    

    NSLog(@"sencond");
    
    [self testSync_01];
    
}

- (void)testSync_01{
    
    NSLog(@"---------------------------------------------");
    
    dispatch_queue_t q = dispatch_queue_create("cn.gcddemo", DISPATCH_QUEUE_CONCURRENT);
    __block bool logon = NO;
    
    
//    dispatch_sync(q, ^{
//        NSLog(@"模拟耗时操作 %@", [NSThread currentThread]);
//        
////        [NSThread sleepfortimeinterval:2.0f];
//        
//        [NSThread sleepForTimeInterval:2.0f];
//        
//        NSLog(@"模拟耗时完成 %@", [NSThread currentThread]);
//        logon = YES;
//    });
    
    
    [NSThread sleepForTimeInterval:2.0f];
    
    NSLog(@"模拟耗时完成 %@", [NSThread currentThread]);
    
    
    dispatch_async(q, ^{
        NSLog(@"登录完成的处理 %@", [NSThread currentThread]);
    });
    
//    [NSThread sleepForTimeInterval:2.0f];
//    
//    NSLog(@"登录完成的处理 %@", [NSThread currentThread]);
}

#pragma mark - 异步Block 

- (void)testSyncBlock:(int(^)(int a, int b))myblock{
    
//    myblock(2, 3);
    
//    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        
//        myblock(2, 3);
//        
//    });
    
    
    dispatch_queue_t serialQueue = dispatch_queue_create("serial", NULL);
    
    dispatch_queue_t concurrent = dispatch_queue_create("concurrent", DISPATCH_QUEUE_CONCURRENT);
    
//    dispatch_sync(serialQueue, ^{
//        
//        myblock(2, 3);
//        
//    });

//    dispatch_sync(concurrent, ^{
//        
//        myblock(2, 3);
//        
//    });
    
#if 1
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

//        myblock(2, 3);
        
//        dispatch_sync(dispatch_get_main_queue(), ^{
//    
//            myblock(2, 3);
//        });
        
        dispatch_queue_t serialQueue = dispatch_queue_create("serial", NULL);
        
        dispatch_queue_t concurrent = dispatch_queue_create("concurrent", DISPATCH_QUEUE_CONCURRENT);
        
        dispatch_sync(serialQueue, ^{
    
            myblock(2, 3);
    
        });
        
//        dispatch_sync(concurrent, ^{
//    
//            myblock(2, 3);
//            
//        });
        
    });
#endif
    
}

- (void)testAsyncBLock1:(int(^)(int a, int b))myblock{
    
//    dispatch_async(dispatch_get_main_queue(), ^{
//       
//        myblock(2, 3);
//    });
    
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        
//        myblock(2, 3);
//    });
    
    dispatch_queue_t serialQueue = dispatch_queue_create("serial", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrent", DISPATCH_QUEUE_CONCURRENT);
    
//    dispatch_async(serialQueue, ^{
//        
//        myblock(2, 3);
//        
//    });
    
    dispatch_async(concurrentQueue, ^{
        
        myblock(2, 3);
        
    });
    
}

- (void)testAsyncFounction{
    
    NSLog(@"--- thread is %@", [NSThread currentThread]);
    
    
    
    dispatch_async(dispatch_get_main_queue(), ^{

        NSLog(@"thread is %@", [NSThread currentThread]);

        NSLog(@"sdfsf");

    });
    
    dispatch_queue_t serialQueue = dispatch_queue_create("serial", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrent", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(serialQueue, ^{
        
        NSLog(@"thread is %@", [NSThread currentThread]);
        
        NSLog(@"sdfsf");
        
    });
    
    dispatch_async(concurrentQueue, ^{
        
        NSLog(@"thread is %@", [NSThread currentThread]);
        
        NSLog(@"sdfsf");
        
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSLog(@"thread is %@", [NSThread currentThread]);
        
        NSLog(@"sdfsf");
        
    });
    
    NSLog(@"sencond");
}

- (void)testSyncFounction{
    
    NSLog(@"--- thread is %@", [NSThread currentThread]);
    
    

    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSLog(@"thread is %@", [NSThread currentThread]);

        NSLog(@"sdfsf");

    });
    
    
    dispatch_queue_t serialQueue = dispatch_queue_create("serial", NULL);
    
    dispatch_queue_t concurrent = dispatch_queue_create("concurrent", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_sync(serialQueue, ^{
        
        NSLog(@"thread is %@", [NSThread currentThread]);
        
        NSLog(@"sdfsf");
        
    });
    
    dispatch_sync(concurrent, ^{
        
        NSLog(@"thread is %@", [NSThread currentThread]);
        
        NSLog(@"sdfsf");
        
    });
    
    
    NSLog(@"sencond");
}


- (void)test{
    
    NSMutableDictionary *mutDic = [@{
                                     
                                     @"name" : @"jack",
                                     @"price" : @"12.8",
                                     @"height" : @34,
                                     @"width" : @11
                                     
                                     } mutableCopy];
    
    NSDictionary *newDic = @{
                             @"name" : @"111",
                             @"price" : @"1111",
                             
                             @"address" : @"zhongshan"
                             
                             };
    
    
    [mutDic addEntriesFromDictionary:newDic];
    
    NSLog(@"the dic is %@", mutDic);
    
    int result = 0;
    
    result = [self testBlockResult:^int(int a, int b) {
        
        NSLog(@" is  first call back ---");
        return a + b;
        
    }];
    
    NSLog(@"the result is %d", result);
    
    
    
    //    self testBlockTypedef:^int(int, int) {
    //        
    //    }
    
    
    self.tyBlock = ^int(int a, int b){
        
        NSLog(@"a + b");
        
        return a + b;
        
    };
    
}

#pragma mark - 同步Block 相当于函数调用
- (int)testBlockResult:(int(^)(int a, int b))myBlcok{
    
    return myBlcok(3, 5);
    
//    dispatch_async(dispatch_get_main_queue(), ^{
//       
//        myBlcok(3, 5);
//    });
//    
//    return 3;
}

- (int)testBlock1:(void(^)(int, int))myBlock{
    return 2;
}

- (int)testBlcokInfo:(void(^)(NSString *, NSString *))myBlock{
    
    return 3;
}

- (int)testBlockTypedef:(Myblock)myblock{
    
    return myblock(2, 4);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    self.tyBlock(5, 7);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值