Skip to content

GCD 扩展使用文档

bang edited this page Dec 14, 2017 · 3 revisions

JSPatch 核心内置了 GCD 常用的四个接口:dispatch_async_main() dispatch_sync_main() dispatch_after() dispatch_async_global_queue(),详见基础用法

若要使用其他GCD接口,可以使用 JPDispatch 扩展,里面封装了以下GCD接口:

dispatch_get_global_queue()
dispatch_get_main_queue()
dispatch_queue_create()

dispatch_async()
dispatch_sync()
dispatch_barrier_async()
dispatch_barrier_sync()
dispatch_apply()

dispatch_group_create()
dispatch_group_async()
dispatch_group_wait()
dispatch_group_notify()
dispatch_group_enter()
dispatch_group_leave()

dispatch_time()

以及一些相关静态常量:

DISPATCH_QUEUE_PRIORITY_HIGH
DISPATCH_QUEUE_PRIORITY_DEFAULT
DISPATCH_QUEUE_PRIORITY_LOW 
DISPATCH_QUEUE_PRIORITY_BACKGROUND
DISPATCH_TIME_NOW
DISPATCH_TIME_FOREVER
DISPATCH_QUEUE_CONCURRENT
DISPATCH_QUEUE_SERIAL

除了 dispatch_time(),其他所有接口定义都与原生接口一致,使用示例:

//JS
require('JPEngine').addExtensions(['JPDispatch']);

var queue = dispatch_queue_create("com.jspatch", DISPATCH_QUEUE_CONCURRENT);
var group = dispatch_group_create();
dispatch_group_async(group, queue, function(){
    console.log("group async 1");
});
dispatch_group_wait(group, dispatch_time(5));
dispatch_group_async(group, queue, function(){
    console.log("group async 2");
});
dispatch_group_notify(group, queue, function(){
    console.log("dispatch_group_notify");
});
Clone this wiki locally