美文网首页iOS Developer
Async流程控制,支持oc链式语法

Async流程控制,支持oc链式语法

作者: 邹浩 | 来源:发表于2016-03-21 14:41 被阅读181次

Async流程控制,支持oc链式语法
Github:https://github.com/shenhai193/Async
#import <OCAsync/Async.h>

Async *async = [[Async alloc]init];

async.userInitiated(0, ^{    // 第一个参数是延时执行时间,单位秒
    // 1
}).main(0, ^{
    // 2
}).background(0, ^{
    // 3
}).main(0, ^ {
    // 4
});

Async是一个oc语言的流程控制框架,用法如上,
支持main,userInteractive,userInitiated,utility,background,customQueue等,第一个参数是延时执行时间,单位为秒。

这里的链式语法指的是点语法,不同于别的语言,oc的方法调用伴随着中括号,如:[[[obj method1] method2] method3:parameter] 。

顺便说下oc链式语法实现思路:

// Test.h
typedef Test*(^Method)(dispatch_block_t block);
@property (copy, nonatomic)Method method;

// Test.m
-(Method)method {
    return ^(dispatch_block_t block) {
        return [[Test alloc]init];
    };
}

Usage:

Test *test = [[Test alloc]init];
test.method(^{
    //...
}).method(^{
    //...
}).method(^ {
    //...
});

相关文章

网友评论

    本文标题:Async流程控制,支持oc链式语法

    本文链接:https://www.haomeiwen.com/subject/oeyklttx.html