After BLOCK Completed only Return

后端 未结 3 1290
别跟我提以往
别跟我提以往 2020-12-22 15:28

May I know is what are the solution to use in order to make the following code working in order.

- (CGFloat)getRowImageHeight
{
    CGFloat defaultHeight   =         


        
3条回答
  •  一整个雨季
    2020-12-22 16:09

    Basically, what you are trying to do is impossible. On the one hand, you have a method from which you need to return a value immediately:

    - (CGFloat)getRowImageHeight
    {
        // ... do some stuff ...
        return height;
    }
    

    On the other hand, in the middle of that code ("do some stuff"), you are performing an asynchronous operation:

    [self configureImageTVCell:self.itemImageTVCell
                         block:^(UIImage *image, BOOL succeeded) {
                             // THIS IS ASYNCHRONOUS
                             if( succeeded )
                                 height     = image.size.height;
                         }];
    

    That means, by definition, that the code in the block will run at some future, unknown time. Meanwhile, your outer method getRowImageHeight has finished and returned its value long ago.

    That is the nature of asynchronous code execution.

    You need to rearchitect your entire approach so that it works in conjunction with your asynchronous code.

    Of course, you have not revealed what you are really trying to do, so I can't tell you how to rearchitect it. But let us pretend for a moment that you are trying to populate a table view's cells with images to be downloaded from the Internet. Well, that is a well-established problem with answers all over the place, including many good explanations on Stack Overflow. So you would read those answers.

提交回复
热议问题