In objective-c, is there some way to check a variable hasn't been released by the time a lambda is called?
-(void) loadImageIntoImageView:(UIImageView*) imgView
{
[MyLibrary getImageFromWebSlowly complete:^(UIImage *img, BOOL success) {
// What if this bit of code is called 50 seconds later,
// and by that time the imgView was dealloc'd or released?
// Eg by that time the user closed the view with the image view on it.
开发者_JAVA百科imgView.image = img;
}];
}
Any suggestions?
(edited for clarity)
In short, don't use __block
or, more complexly, assign nil
to the __block
variable when it is deallocated.
As written, there is no need for the __block
keyword in that code unless you explicitly want a weak reference to imgView
(which you probably don't).
I think you mean "deallocated" rather than released, and with no garbage collection on iOS, this isn't possible. You should use some other method to determine whether the UIImageView
has gone away.
Also, I don't think __block
does what you think it does; consider reading about how block variables work.
精彩评论