延迟返回值而不是整个方法 [英] Delay the return value instead of whole method

查看:105
本文介绍了延迟返回值而不是整个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以仅延迟返回值,而不是像整个方法一样:

Is it possible to delay only the return value and not the whole method like:

-(CGFloat)getValue
{
    // code implementation
    return floatvalue; // return float value with some delay.
}


推荐答案

使用块来回拨带延迟的返回值;

Use a block to call back with return value with delay;

- (void)ayncGetValue:(void(^)(id value))returnBlock {

  dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 10.f * NSEC_PER_SEC); // delay 10 seconds
  dispatch_after(popTime, dispatch_get_main_queue(), ^{
    returnBlock(@"hello block");
  });
}
// Usage
[obj ayncGetValue:^(id value) {
  // continue  
}];

这篇关于延迟返回值而不是整个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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