May I know is what are the solution to use in order to make the following code working in order.
- (CGFloat)getRowImageHeight
{
CGFloat defaultHeight = 300.f;
__block CGFloat height = defaultHeight;
[self configureImageTVCell:self.itemImageTVCell
block:^(UIImage *image, BOOL succeeded) {
if( succeeded )
height = image.size.height;
}];
return height;
}
I would expect the BLOCK to complete first before the return height, it is because I wanted to get the latest height from the completion of BLOCK.
I've tried a few solution by using dispatch_semaphore_t, but it causes deadlock where the simulator actually hang.
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[self configureImageTVCell:self.itemImageTVCell
block:^(UIImage *image, BOOL succeeded) {
if( succeeded )
height = image.size.height;
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
Running out of idea for this already, need help indeed.
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:
On the other hand, in the middle of that code ("do some stuff"), you are performing an asynchronous operation:
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.