这里使用了两种方式:

  • GCD – 阻塞当前线程
  • RunLoop – 不阻塞当前线程

看下对应 API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
typedef void (^k_async_task_callback)(id, ...);
typedef void (^k_async_task)(id obj, k_async_task_callback callback);

typedef void (^k_async_task_interrupt_handler)(void);
typedef void (^k_async_task_interruptable)(id obj, k_async_task_interrupt_handler interrupt_handler, k_async_task_callback callback);


/// GCD 阻塞当前线程等待
void k_async_to_sync(k_async_task task, k_async_task_callback callback);
void k_async_to_sync_interruptable(k_async_task_interruptable task, k_async_task_callback callback);

/// RunLoop 不阻塞当前线程
void k_async_to_sync_nonblocking(k_async_task task, k_async_task_callback callback);
void k_async_to_sync_nonblocking_interruptable(k_async_task_interruptable task, k_async_task_callback callback);

具体使用见如下 Demo 测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
- (void)gcd_test {
NSLog(@"gcd_test begin -----");

k_async_task_callback async_task_callback = ^(id obj, ...) {

va_list args;
va_start(args, obj);

id string = va_arg(args, id);
while (string) {
string = va_arg(args, id);
}
va_end(args);

NSLog(@"gcd_test task complete -----");
};

k_async_task task = ^(id obj, k_async_task_callback callback) {

// 开始执行异步费时操作
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{

// 在异步操作完成后,必须调用 callback: 第一个参数 obj 必须传
// 这里 后面可以跟多个参数,不过在获取时注意参数个数
callback(obj, @"aaa", nil);
});
};
k_async_to_sync(task, async_task_callback);
NSLog(@"gcd_test end -----");



NSLog(@"gcd_test111 begin -----");

__block k_async_task_interrupt_handler custom_interrupt_handler;
k_async_task_interruptable interrupt_task = ^(id obj, k_async_task_interrupt_handler interrupt_handler, k_async_task_callback callback) {
// 保存 interrupt handler,外界可以随时打断 task
custom_interrupt_handler = [interrupt_handler copy];

// 开始执行异步费时操作
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{

callback(obj, @"aaa", nil);
});
};
k_async_to_sync_interruptable(interrupt_task, async_task_callback);

NSLog(@"gcd_test111 end -----");
}





- (void)runLoop_test {
NSLog(@"runLoop_test begin -----");

k_async_task_callback async_task_callback = ^(id obj, ...) {

va_list args;
va_start(args, obj);

id string = va_arg(args, id);
while (string) {
string = va_arg(args, id);
}
va_end(args);

NSLog(@"runLoop_test task complete -----");
};


k_async_task task = ^(id obj, k_async_task_callback callback) {

// 开始执行异步费时操作
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{

// 在异步操作完成后,必须调用 callback: 第一个参数 obj 必须传
// 这里 后面可以跟多个参数,不过在获取时注意参数个数
callback(obj, @"aaa", nil);
});
};
k_async_to_sync_nonblocking(task, async_task_callback);
NSLog(@"runLoop_test end -----");



NSLog(@"runLoop_test111 begin -----");

__block k_async_task_interrupt_handler custom_interrupt_handler;
k_async_task_interruptable interrupt_task = ^(id obj, k_async_task_interrupt_handler interrupt_handler, k_async_task_callback callback) {
// 保存 interrupt handler,外界可以随时打断 task
custom_interrupt_handler = [interrupt_handler copy];

// 开始执行异步费时操作
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), queue, ^{

callback(obj, @"aaa", nil);
});
};
k_async_to_sync_nonblocking_interruptable(interrupt_task, async_task_callback);

NSLog(@"runLoop_test111 end -----");
}

Demo地址