nicklockwood / FXBlurView

[DEPRECATED]
Other
4.94k stars 713 forks source link

iOS 11 - Main Thread Checker: UI API called on a background thread: -[UIView layer] #143

Open codegastudio opened 6 years ago

codegastudio commented 6 years ago

Hi,

With Xcode 9 and device with iOS 11 When i activate blur i get the following message in logger

Main Thread Checker: UI API called on a background thread: -[UIView layer] PID: 2255, TID: 1737593, Thread name: (none), Queue name: com.apple.root.default-qos, QoS: 21 Backtrace: 4 MyApp 0x00000001004a9b94 -[FXBlurView blurLayer] + 40 5 MyApp 0x00000001004a976c -[FXBlurView blurRadius] + 40 6 MyApp 0x00000001004abb8c __46-[FXBlurView updateAsynchronously:completion:]_block_invoke + 68 7 libdispatch.dylib 0x000000010082149c _dispatch_call_block_and_release + 24 8 libdispatch.dylib 0x000000010082145c _dispatch_client_callout + 16 9 libdispatch.dylib 0x000000010082d56c _dispatch_queue_override_invoke + 980 10 libdispatch.dylib 0x0000000100832b54 _dispatch_root_queue_drain + 616 11 libdispatch.dylib 0x0000000100832880 _dispatch_worker_thread3 + 136 12 libsystem_pthread.dylib 0x0000000182c5f130 _pthread_wqthread + 1268 13 libsystem_pthread.dylib 0x0000000182c5ec30 start_wqthread + 4

- (void)updateAsynchronously:(BOOL)async completion:(void (^)())completion
{
    if ([self shouldUpdate])
    {
        UIImage *snapshot = [self snapshotOfUnderlyingView];
        if (async)
        {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

                UIImage *blurredImage = [self blurredSnapshot:snapshot radius:self.blurRadius];
                dispatch_sync(dispatch_get_main_queue(), ^{

                    [self setLayerContents:blurredImage];
                    if (completion) completion();
                });
            });
        }
        else
        {
            [self setLayerContents:[self blurredSnapshot:snapshot radius:[self blurPresentationLayer].blurRadius]];
            if (completion) completion();
        }
    }
    else if (completion)
    {
        completion();
    }
}

Possible to try with

...
dispatch_async(dispatch_get_main_queue(), ^{
        ...
});
...

Best regards,

mixiu7992 commented 6 years ago

i encounter this problem and some can fix it?

floschliep commented 6 years ago

I fixed this by getting the blur radius before entering the background queue:

CGFloat blurRadius = self.blurRadius;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    UIImage *blurredImage = [self blurredSnapshot:snapshot radius:blurRadius];
    dispatch_sync(dispatch_get_main_queue(), ^{

        [self setLayerContents:blurredImage];
        if (completion) completion();
    });
});
zerocc2017 commented 3 years ago

I fixed this by getting the blur radius before entering the background queue:

CGFloat blurRadius = self.blurRadius;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    UIImage *blurredImage = [self blurredSnapshot:snapshot radius:blurRadius];
    dispatch_sync(dispatch_get_main_queue(), ^{

        [self setLayerContents:blurredImage];
        if (completion) completion();
    });
});

this bug is why? can explain for me。thx