有一种Block叫Callback,有一种Callback叫CompletionHandler

移动开发 iOS
iOS10推送部分的API,大量使用了 CompletionHandler 这种命名方式,那么本文我们将对比下这种 Block 的特殊性,以便更好的理解和在自己的项目中实践 CompletionHandler 样式的 Blcok。

【引言】iOS10推送部分的API,大量使用了 CompletionHandler 这种命名方式,那么本文我们将对比下这种 Block 的特殊性,以便更好的理解和在自己的项目中实践 CompletionHandler 样式的 Blcok。

正文

我们作为开发者去集成一个 Lib (也可以叫轮子、SDK、下文统一叫 Lib)时,我们会发现我们遇到的 Block, 按照功能的角度划分,其实可以分为这几种:

  • Lib 通知开发者,Lib操作已经完成。一般命名为 Callback
  • 开发者通知 Lib,开发者的操作已经完成。一般可以命名为 CompletionHandler。

这两处的区别: 前者是 “Block 的执行”,后者是 “Block 的填充”。

Callback vs CompletionHandler 命名与功能的差别,Apple 也没有明确的编码规范指出过,只不过如果按照“执行与填充”的功能划分的话,callback 与 completionHandler 的命名可以区分开来对待。同时也方便调用者理解 block 的功能。但总体来说,Apple 官方的命名中,“Block 填充“这个功能一般都会命名为 “completionHandler”,“Block 执行”这个功能大多命名为了“callback” ,也有少部分命名为了 “completionHandler”。

比如:

NSURLSession 中,下面的函数将 “callback” 命名为了 “completionHandler”:

  1. - (NSURLSessionDataTask *)dataTaskWithURL:(NSURL *)url completionHandler:(void (^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler; 

我们常常见到 CompletionHandler 被用到了***种场景,而***种场景“Block 执行”命名为 Callback 则更合适。

不是所有 Block 都适合叫做 CompletionHandler

一般情况下,CompletionHandler 的设计往往考虑到多线程操作,于是,你就完全可以异步操作,然后在线程结束时执行该 CompletionHandler,下文的例子中会讲述下 CompletionHandler 方式在多线程场景下的一些优势。

CompletionHandler + Delegate 组合

在 iOS10 中新增加的 UserNotificaitons 中大量使用了这种 Block,比如:

  1. - (void)userNotificationCenter:(UNUserNotificationCenter *)center 
  2. didReceiveNotificationResponse:(UNNotificationResponse *)response 
  3.          withCompletionHandler:(void (^)(void))completionHandler; 

 

文档 对 completionHandler 的注释是这样的:

  1. The block to execute when you have finished processing the user’s response. You must execute this block from your method and should call it as quickly as possible. The block has no return value or parameters. 

同样在这里也有应用:

  1. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task 
  2. didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge 
  3. completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler; 

 

还有另外一个也非常普遍的例子(Delegate 方式使用URLSession 时候必不可少的 4个代理函数之一 )

  1. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask 
  2.                                  didReceiveResponse:(NSURLResponse *)response 
  3.                                   completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler; 

 

在代理方法实现代码里面,若是不执行 completionHandler(NSURLSessionResponseAllow) 话,http请求就终止了。

CompletionHandler + Block 组合

函数中将函数作为参数或者返回值,就叫做高阶函数。

按照这种定义,Block 中将 Block 作为参数,这也就是高阶函数。

结合实际的应用场景来看一个例子:

如果有这样一个需求:

拿我之前的一个 IM 项目 ChatKit-OC (开源的,下面简称 ChatKit)为例,当你的应用想要集成一个 IM 服务时,可能这时候,你的 APP 已经上架了,已经有自己的注册、登录等流程了。用 ChatKit 进行聊天很简单,只需要给 ChatKit 一个 id 就够了。聊天是正常了,但是双方只能看到一个id,这样体验很不好。但是如何展示头像、昵称呢?于是就设计了这样一个接口,-setFetchProfilesBlock: 。

这是上层(APP)提供用户信息的 Block,由于 ChatKit 并不关心业务逻辑信息,比如用户昵称,用户头像等。用户可以通过 ChatKit 单例向 ChatKit 注入一个用户信息内容提供 Block,通过这个用户信息提供 Block,ChatKit 才能够正确的进行业务逻辑数据的绘制。

示意图如下:

 

 

 

 

具体实现如下:

方法定义如下:

  1. /*! 
  2. *  @brief The block to execute with the users' information for the userIds. Always execute this block at some point when fetching profiles completes on main thread. Specify users' information how you want ChatKit to show. 
  3. *  @attention If you fetch users fails, you should reture nil, meanwhile, give the error reason. 
  4. */ 
  5. typedef void(^LCCKFetchProfilesCompletionHandler)(NSArray> *users, NSError *error); 
  6.   
  7. /*! 
  8. *  @brief When LeanCloudChatKit wants to fetch profiles, this block will be invoked. 
  9. *  @param userIds User ids 
  10. *  @param completionHandler The block to execute with the users' information for the userIds. Always execute this block at some point during your implementation of this method on main thread. Specify users' information how you want ChatKit to show. 
  11. */ 
  12. typedef void(^LCCKFetchProfilesBlock)(NSArray *userIds, LCCKFetchProfilesCompletionHandler completionHandler); 
  13.   
  14. @property (nonatomic, copy) LCCKFetchProfilesBlock fetchProfilesBlock; 
  15.   
  16. /*! 
  17. *  @brief Add the ablitity to fetch profiles. 
  18. *  @attention  You must get peer information by peer id with a synchronous implementation. 
  19. *              If implemeted, this block will be invoked automatically by LeanCloudChatKit for fetching peer profile. 
  20. */ 
  21. - (void)setFetchProfilesBlock:(LCCKFetchProfilesBlock)fetchProfilesBlock; 

 

用法如下所示:

  1. #warning 注意:setFetchProfilesBlock 方法必须实现,如果不实现,ChatKit将无法显示用户头像、用户昵称。以下方法循环模拟了通过 userIds 同步查询 users 信息的过程,这里需要替换为 App 的 API 同步查询 
  2.     [[LCChatKit sharedInstance] setFetchProfilesBlock:^(NSArray *userIds, 
  3.                              LCCKFetchProfilesCompletionHandler completionHandler) { 
  4.          if (userIds.count == 0) { 
  5.              NSInteger code = 0; 
  6.              NSString *errorReasonText = @"User ids is nil"
  7.              NSDictionary *errorInfo = @{ 
  8.                                          @"code":@(code), 
  9.                                          NSLocalizedDescriptionKey : errorReasonText, 
  10.                                          }; 
  11.              NSError *error = [NSError errorWithDomain:NSStringFromClass([self class]) 
  12.                                                   code:code 
  13.                                               userInfo:errorInfo]; 
  14.   
  15.              !completionHandler ?: completionHandler(nil, error); 
  16.              return
  17.          } 
  18.   
  19.          NSMutableArray *users = [NSMutableArray arrayWithCapacity:userIds.count]; 
  20. #warning 注意:以下方法循环模拟了通过 userIds 同步查询 users 信息的过程,这里需要替换为 App 的 API 同步查询 
  21.   
  22.          [userIds enumerateObjectsUsingBlock:^(NSString *_Nonnull clientId, NSUInteger idx, 
  23.                                                BOOL *_Nonnull stop) { 
  24.              NSPredicate *predicate = [NSPredicate predicateWithFormat:@"peerId like %@", clientId]; 
  25.              //这里的LCCKContactProfiles,LCCKProfileKeyPeerId都为事先的宏定义, 
  26.              NSArray *searchedUsers = [LCCKContactProfiles filteredArrayUsingPredicate:predicate]; 
  27.              if (searchedUsers.count > 0) { 
  28.                  NSDictionary *user = searchedUsers[0]; 
  29.                  NSURL *avatarURL = [NSURL URLWithString:user[LCCKProfileKeyAvatarURL]]; 
  30.                  LCCKUser *user_ = [LCCKUser userWithUserId:user[LCCKProfileKeyPeerId] 
  31.                                                        name:user[LCCKProfileKeyName] 
  32.                                                   avatarURL:avatarURL 
  33.                                                    clientId:clientId]; 
  34.                  [users addObject:user_]; 
  35.              } else { 
  36.                  //注意:如果网络请求失败,请至少提供 ClientId! 
  37.                  LCCKUser *user_ = [LCCKUser userWithClientId:clientId]; 
  38.                  [users addObject:user_]; 
  39.              } 
  40.          }]; 
  41.          // 模拟网络延时,3秒 
  42.          //         sleep(3); 
  43.   
  44. #warning 重要:completionHandler 这个 Bock 必须执行,需要在你**获取到用户信息结束**后,将信息传给该Block! 
  45.          !completionHandler ?: completionHandler([users copy], nil); 
  46.      }]; 

 

对于以上 Fetch 方法的这种应用场景,其实用方法的返回值也可以实现,但是与 CompletionHandler 相比,无法自由切换线程是个弊端。 

责任编辑:庞桂玉 来源: iOS大全
相关推荐

2016-09-27 09:44:33

云计算柔性云运维

2017-11-12 21:32:52

戴尔

2014-02-25 10:11:00

2016-01-08 09:49:19

DockerDocker案例云应用开发

2015-09-22 13:40:50

互联网业务运维

2016-10-14 06:45:23

网络安全安全运维

2013-03-26 22:32:48

2020-12-23 10:10:23

Pythonweb代码

2022-07-07 10:33:27

Python姿势代码

2022-06-22 09:44:41

Python文件代码

2021-09-09 08:55:49

节点累加树二叉

2020-12-09 10:15:34

Pythonweb代码

2012-01-17 11:02:39

2015-08-03 09:36:01

赛迪翻译

2023-06-02 15:26:37

光纤综合布线

2015-08-31 09:27:21

语言界面UI

2017-06-22 16:46:45

2019-08-29 16:05:06

物联网

2019-01-21 08:30:01

年终奖员工人性化

2015-01-21 15:35:58

开源
点赞
收藏

51CTO技术栈公众号