software-mariodiana
12/3/2017 - 12:59 PM

Ensure UIView bounds method is called from main thread.

Ensure UIView bounds method is called from main thread.

- (CGRect)bounds
{
    // The architecture of the framework is such that bounds is sometimes accessed from a
    // non-main thread. iOS 11 complains about this sort of thing.
    __block CGRect theBounds = CGRectNull;
    
    if ([NSThread isMainThread])
    {
        // Trying to grab the main thread if you're on the main thread causes deadlock.
        theBounds = [super bounds];
    }
    else
    {
        // Submits a block object for execution on a dispatch queue and waits until that block completes.
        dispatch_sync(dispatch_get_main_queue(), ^{
            theBounds = [super bounds];
        });
    }
    
    return theBounds;
}