如果我的基于块的API刚刚完成或成功和失败处理程序? [英] Should my Block-based API have just completion or both success and failure handlers?

查看:122
本文介绍了如果我的基于块的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;

我总是使用第二种方法,但什么是每个选项的PRO /缺点是什么?你有什么正常使用,为什么?

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

推荐答案

两者都是精细的图案(我了投票这两个FIRO的和约翰的答案,因为 - 是的,这是一个品味的问题,这两个他们的答案都是现货上),但也有几个明显的优势与单块去,在我的经验。约翰·伍兹提出了对API的味道非常好的一点,但我会声称网络操作的总是在完成的(除非它有没有超时,这是一个不同的类的bug),而的成功/失败的方式是经常不太要么/或

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.


  • 这消除了重复是共同的拆除任务在完成后,无论成功还是失败块之间的code。

  • 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 ** 模式纯粹是要么/或,使用两种块模式的好处之一是,这可能是前pressed。使用单块完成的另一个优点是,你避免重复的逻辑。

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天全站免登陆