我的基于块的 API 应该只有完成处理程序还是成功和失败处理程序? [英] Should my Block-based API have just completion or both success and failure handlers?

查看:17
本文介绍了我的基于块的 API 应该只有完成处理程序还是成功和失败处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ObjC 中设计基于块的 API 时,哪种方法更好,一个或两个完成块,成功和失败各一个?

When designing a block-based API in ObjC, which approach is better, one completion block or two, one each for success and failure?

假设我们有一种方法可以异步地将某些内容检索到一个块中,其中一个完成块将是:

Let's say we have a method retrieving something asynchronously to a block, with one completion block it would be:

- (void) retrieveSomethingCompletion:(void (^)(id retrievedObject, NSError *error))completionBlock;

还有成功/失败块(AFNetworking 风格):

And with success/failure blocks (AFNetworking style):

- (void) retrieveSomethingSuccess:(void(^)(id retrievedObject))successBlock failure:(void(^)(NSError *error))failureBlock;

我总是使用第二种方法,但每个选项的优缺点是什么?您通常使用什么?为什么?

I always use the second approach, but what are the pro/cons of each option? What do you use normally and why?

推荐答案

两者都是很好的模式(我对 Firo 和 John 的答案都投了赞成票,因为——是的,这是一个品味问题,他们的两个答案都恰到好处),但根据我的经验,使用单个块有几个明显的优势.John Woods 对 API 的风格"提出了一个很好的观点,尽管我认为网络操作总是完成(除非它没有超时,这是一种不同的错误类别)并且成功/失败模式并不完全是非此即彼.

Both are fine patterns (and I up voted both Firo's and John's answers because -- yes, it is a matter of taste and both of their answers are spot on), but there are several distinct advantages to going with a single block, in my experience. John Woods raises an excellent point about the "flavor" of the API, though I would claim that a network operation always completes (unless it has no timeout, which is a different class of bug) and that the success/failure modality is oft not quite either/or.

  • 它消除了在块之间复制代码的必要性,因为无论成功还是失败,在完成任务时都会出现这种情况.

  • It eliminates having to duplicate the code between the blocks that is common to tearing down the task on completion regardless of success or failure.

它在调度任务和知道任务何时完成之间提供了一个单一的概念执行流程.此任务完成后,将调用完成块.

It provides a single conceptual execution flow between scheduling the task and knowing when the task is completed. When this task is completed, the completion block will be called.

在某些情况下,失败实际上可能会产生应以成功路径等方式处理的数据.在少数情况下,成功完成实际上可能会带来错误.虽然方法上的 NSError** 模式纯粹是要么/要么,使用任一块模式的优点之一是可以表达.使用单个完成块的另一个优点是可以避免重复的逻辑.

In some situations, a failure may actually produce data that should be processed in a fashion like the success path. In fewer situations, a successful completion may actually carry along an error. While the NSError** pattern on methods is purely either/or, one of the advantages of using either block pattern is that this can be expressed. The further advantage of using a single completion block is that you avoid duplicated logic.

多个块作为方法的参数非常丑陋和烦人.编码模式是只将一个块传递给一个方法,并始终将该块作为最后一个参数是有原因的.不幸的是,即使是系统 API 也不会始终遵循这种模式,尽管使用仅限于主要块往往很简单的情况.大部分.

Multiple blocks as parameters to methods is flat out ugly and annoying. There is a reason why the coding pattern is only pass one block to a method and always make that block the last parameter. Unfortunately, even the system APIs don't consistently follow this pattern, though the use is limited mostly to cases where the blocks tend to be simple. Mostly.

它避免了这样的废话:

 [foo doSomething: ^{
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
  } andSomethingElse: ^{
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
       .... lots of code ... 
   }];

这篇关于我的基于块的 API 应该只有完成处理程序还是成功和失败处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆