Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UIView.layer must be used from main thread only #14

Open
renanstig opened this issue Feb 23, 2018 · 6 comments
Open

UIView.layer must be used from main thread only #14

renanstig opened this issue Feb 23, 2018 · 6 comments

Comments

@renanstig
Copy link

Hi,

I've been using your library and it's been working really well, but lately I've tried to fix this warning without success, so I was wondering if you could find a solution for this bug.

Best regards,
Renan.

@yichizhang
Copy link
Owner

Could you provide more information, such as situations when this error occurs, and also the console logs?

@renanstig
Copy link
Author

renanstig commented Mar 12, 2018

Sure,

I could find some logs here:

According to Xcode, this is the function being accessed from background thread
screen shot 2018-03-12 at 15 27 04

PID: 4925, TID: 1260877, Thread name: (none), Queue name: com.apple.root.default-qos, QoS: 21
Backtrace:
4   DocoToolsDemo                       0x000000010254c4ec _T013DocoToolsDemo11JCTiledViewC10tiledLayerAA0dG0CyF + 44
5   DocoToolsDemo                       0x000000010254cf9d _T013DocoToolsDemo11JCTiledViewC4drawySC6CGRectVF + 205
6   DocoToolsDemo                       0x000000010254d6bf _T013DocoToolsDemo11JCTiledViewC4drawySC6CGRectVFTo + 95
7   UIKit                               0x0000000108c7c1c5 -[UIView(CALayerDelegate) drawLayer:inContext:] + 487
8   QuartzCore                          0x00000001079d4254 -[CALayer drawInContext:] + 267
9   QuartzCore                          0x000000010791e764 _ZL18tiled_layer_renderP16_CAImageProviderjjjjPv + 1817
10  QuartzCore                          0x00000001079aa88e _ZL21CAImageProviderThreadPjb + 789
11  libdispatch.dylib                   0x000000010e62833d _dispatch_client_callout + 8
12  libdispatch.dylib                   0x000000010e62d251 _dispatch_queue_override_invoke + 1458
13  libdispatch.dylib                   0x000000010e634102 _dispatch_root_queue_drain + 772
14  libdispatch.dylib                   0x000000010e633da0 _dispatch_worker_thread3 + 132
15  libsystem_pthread.dylib             0x000000010eaed169 _pthread_wqthread + 1387
16  libsystem_pthread.dylib             0x000000010eaecbe9 start_wqthread + 13
2018-03-12 15:25:36.019563+0100 DocoToolsDemo[4925:1260877] [reports] Main Thread Checker: UI API called on a background thread: -[UIView layer]
PID: 4925, TID: 1260877, Thread name: (none), Queue name: com.apple.root.default-qos, QoS: 21
Backtrace:
4   DocoToolsDemo                       0x000000010254c4ec _T013DocoToolsDemo11JCTiledViewC10tiledLayerAA0dG0CyF + 44
5   DocoToolsDemo                       0x000000010254cf9d _T013DocoToolsDemo11JCTiledViewC4drawySC6CGRectVF + 205
6   DocoToolsDemo                       0x000000010254d6bf _T013DocoToolsDemo11JCTiledViewC4drawySC6CGRectVFTo + 95
7   UIKit                               0x0000000108c7c1c5 -[UIView(CALayerDelegate) drawLayer:inContext:] + 487
8   QuartzCore                          0x00000001079d4254 -[CALayer drawInContext:] + 267
9   QuartzCore                          0x000000010791e764 _ZL18tiled_layer_renderP16_CAImageProviderjjjjPv + 1817
10  QuartzCore                          0x00000001079aa88e _ZL21CAImageProviderThreadPjb + 789
11  libdispatch.dylib                   0x000000010e62833d _dispatch_client_callout + 8
12  libdispatch.dylib                   0x000000010e62d251 _dispatch_queue_override_invoke + 1458
13  libdispatch.dylib                   0x000000010e634102 _dispatch_root_queue_drain + 772
14  libdispatch.dylib                   0x000000010e633da0 _dispatch_worker_thread3 + 132
15  libsystem_pthread.dylib             0x000000010eaed169 _pthread_wqthread + 1387
16  libsystem_pthread.dylib             0x000000010eaecbe9 start_wqthread + 13```




@renanstig
Copy link
Author

renanstig commented Mar 12, 2018

This problem started to happen only in iOS 11.
In order to make it happen you just have to load a tile from a web service, it doesn't crash the app all the time, but I get crashes from crashlytics and it doesn't make much sense to me and I've been trying to fix it since then

@renanstig
Copy link
Author

Hey,

I'm sorry for bothering you with that, I've been trying to fix this issue, but no success so far. Could you find a solution for this?

Best regards,

@yichizhang
Copy link
Owner

It's probably related to how to download the tile? Make sure that after downloading the tile, you update the UI on the main thread.

@dhoerl
Copy link

dhoerl commented Jul 28, 2020

I'm adding this (mostly) for future readers. My current company is using this source, and we are getting occasional crashes just like the one above. However, that the tile draws on a background thread is expected: if you look at the original repository, there are links to various sites that explain CATileLayer behavior, including this by Apple:

https://developer.apple.com/library/archive/qa/qa1637/_index.html

Apple says that yes, they do the rendering on background threads, and a long time ago (pre ios4) UIKit was not background safe. However, they say that's fixed now.

For me, the safest approach would be to modify the source to return a CGImage, not a UIImage, and draw it in the context. Its quite possible Apple missed some edge cases and that's what's causing the crashes. The rate at which we get them is very low, but they dribble in every week.

Take a look at the Apple link above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants