Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

batch_await #92

Open
yunjinghui123 opened this issue Aug 13, 2019 · 2 comments
Open

batch_await #92

yunjinghui123 opened this issue Aug 13, 2019 · 2 comments

Comments

@yunjinghui123
Copy link

EB9CE5DB-B9F8-4A75-964F-A58C97488898
在NSArray *co_batch_await(NSArray * awaitableList)函数中为什么会将错误替换进result?

我的场景是使用batch_await调用好几个接口,然后拿到结果刷新页面,但是如果其中一个接口调用失败我将调用reject
NSError *err = [NSError errorWithDomain:@"data error" code:-1 userInfo:nil];
[promise reject:err];
这样,而调用batch_await之后,由于
NSError *error = co_getError();
if (error) {
[result replaceObjectAtIndex:i withObject:error];
}
前面数组中得到的结果也是有error,我在结果取值result[1]的时候并没有得到想要的结果,而是error,这样再走下面的逻辑程序就会有问题,为什么不能将错误放入co_getError中,
想await中那样,最后取得错误另外处理,否则每个result[1], result[2],我都需要判断返回的结果是否有错,再走正常逻辑
NSArray *recommentArray = result[1];
if (![recommentArray isKindOfClass:[NSError class]]) {
goodsObj.recommendArray = recommentArray;
}

    NSMutableArray *goodsColorInfo = result[2];
    if (![goodsColorInfo isKindOfClass:[NSError class]]) {
        [self getHuiMaiColorAndStyleInfo:goodsColorInfo];
    }

这样比较繁琐

@NianJi
Copy link
Collaborator

NianJi commented Aug 14, 2019

由于oc里面不能适用异常,所以error处理逻辑是比较麻烦。

在你这个场景中的确是按你说的比较方便。但是也可能存在batch并发并不因某个任务失败而全盘失败的case。 batch_await 在coobjc里面算是个util封装,你可以根据你自身的情况进行封装。

@yunjinghui123
Copy link
Author

为了避免错误对正常的业务逻辑造成影响,我用元组回调了错误和正常数据,做了区分
NSMutableArray<NSError *> *errors = [NSMutableArray array];
NSError *error;
co_unpack(&commentMod, &error) = result.firstObject;
多个接口的错误没有逐一展示,而是取最后一条显示,也能解决目前的问题,貌似没有什么好的解决方案了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants