Reducing load on the main thread when using dispatch_get_main_queue()
-
25-10-2019 - |
문제
I'm generating a lot of thumbnails in my iPhone app using GCD. I have something that look like this :
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// generate thumbnail
// store thumbnail
dispatch_async(dispatch_get_main_queue(), ^{
// display thumbnail (cellForRowAtIndexPath and cell.imageView.image = ..
});
);
It works pretty well, but I would like to lower the priority for the display block, in order to have a more responsive UI thread.
해결책
The main queue (associated with the main thread) doesn't have a priority. You can't change its priority.
Anyway, you may instead post a NSNotification
using the NSPostWhenIdle
posting style. This will allow your code to be executed only when your app have some idle time. See here in the documentation / dedicated Programming Guide about such techniques.
제휴하지 않습니다 StackOverflow