GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Change the default callback queue policy to SafeAsyncMainThread, don't need main queue check at all by dreampiggy · Pull Request #3797 · SDWebImage/SDWebImage

Change the default callback queue policy to SafeAsyncMainThread, don't need main queue check at all #3797

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.

Sign up for GitHub

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

Merged
dreampiggy merged 1 commit into SDWebImage:master from dreampiggy:feature/remove_asyncMainQueue
Feb 22, 2025

Uh oh!

There was an error while loading. Please reload this page.

Merged

Change the default callback queue policy to SafeAsyncMainThread, don't need main queue check at all #3797

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
ee319fc
Change the default callback queue policy to SafeAsyncMainThread, don'…
dreampiggy Feb 21, 2025
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

8 changes: 3 additions & 5 deletions 8 SDWebImage/Core/SDCallbackQueue.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ typedef NS_ENUM(NSUInteger, SDCallbackPolicy) {
SDCallbackPolicyDispatch = 1,
/// Ignore any async/sync and just directly invoke `block` in current queue (without `dispatch_async`/`dispatch_sync`)
SDCallbackPolicyInvoke = 2,
/// Ensure callback in main queue (no gurantee on main thread). Do `dispatch_async` if the current queue is not main queue; else do invoke `block`. Never use `dispatch_sync`, suitable for general UI-related code
SDCallbackPolicySafeAsyncMainQueue = 3,
/// Ensure callback in main thread. Do `dispatch_async` if the `NSThread.isMainTrhead == true` ; else do invoke `block`. Never use `dispatch_sync`, suitable for special UI-related code
Comment thread

@dreampiggy dreampiggy Feb 24, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems the comment is not correct. The code implementation shows “When current thread is not main thread, do dispatch_async”

TODO: fix later @dreampiggy

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
SDCallbackPolicySafeAsyncMainThread = 4,
SDCallbackPolicySafeAsyncMainThread = 3,
};

/// SDCallbackQueue is a wrapper used to control how the completionBlock should perform on queues, used by our `Cache`/`Manager`/`Loader`.
/// Useful when you call SDWebImage in non-main queue and want to avoid it callback into main queue, which may cause issue.
@interface SDCallbackQueue : NSObject

/// The main queue. This is the default value, has the same effect when passing `nil` to `SDWebImageContextCallbackQueue`
/// The policy defaults to `SDCallbackPolicySafeAsyncMainQueue`
/// The policy defaults to `SDCallbackPolicySafeAsyncMainThread`
@property (nonnull, class, readonly) SDCallbackQueue *mainQueue;

/// The caller current queue. Using `dispatch_get_current_queue`. This is not a dynamic value and only keep the first call time queue.
Expand All @@ -52,7 +50,7 @@ typedef NS_ENUM(NSUInteger, SDCallbackPolicy) {

/// Submits a block for execution and returns after that block finishes executing.
/// - Parameter block: The block that contains the work to perform.
- (void)sync:(nonnull dispatch_block_t)block;
- (void)sync:(nonnull dispatch_block_t)block API_DEPRECATED("No longer use, may cause deadlock when misused", macos(10.10, 10.10), ios(8.0, 8.0), tvos(9.0, 9.0), watchos(2.0, 2.0));;

/// Schedules a block asynchronously for execution.
/// - Parameter block: The block that contains the work to perform.
Expand Down
27 changes: 4 additions & 23 deletions 27 SDWebImage/Core/SDCallbackQueue.m
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,7 @@ @interface SDCallbackQueue ()

@end

static inline void SDSafeMainQueueAsync(dispatch_block_t _Nonnull block) {
if (NSThread.isMainThread) {
// Match exists `dispatch_main_async_safe` behavior
const char *currentLabel = dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL);
const char *mainLabel = dispatch_queue_get_label(dispatch_get_main_queue());
if (mainLabel == currentLabel) {
block();
return;
}
}
dispatch_async(dispatch_get_main_queue(), block);
}

static inline void SDSafeMainThreadAsync(dispatch_block_t _Nonnull block) {
static inline void SDSafeAsyncMainThread(dispatch_block_t _Nonnull block) {
if (NSThread.isMainThread) {
block();
} else {
Expand Down Expand Up @@ -71,7 +58,7 @@ - (instancetype)initWithDispatchQueue:(dispatch_queue_t)queue {

+ (SDCallbackQueue *)mainQueue {
SDCallbackQueue *queue = [[SDCallbackQueue alloc] initWithDispatchQueue:dispatch_get_main_queue()];
queue->_policy = SDCallbackPolicySafeAsyncMainQueue;
queue->_policy = SDCallbackPolicySafeAsyncMainThread;
return queue;
}

Expand Down Expand Up @@ -99,11 +86,8 @@ - (void)sync:(nonnull dispatch_block_t)block {
case SDCallbackPolicyInvoke:
block();
break;
case SDCallbackPolicySafeAsyncMainQueue:
SDSafeMainQueueAsync(block);
break;
case SDCallbackPolicySafeAsyncMainThread:
SDSafeMainThreadAsync(block);
SDSafeAsyncMainThread(block);
break;
default:
NSCAssert(NO, @"unexpected policy %tu", self.policy);
Expand All @@ -122,11 +106,8 @@ - (void)async:(nonnull dispatch_block_t)block {
case SDCallbackPolicyInvoke:
block();
break;
case SDCallbackPolicySafeAsyncMainQueue:
SDSafeMainQueueAsync(block);
break;
case SDCallbackPolicySafeAsyncMainThread:
SDSafeMainThreadAsync(block);
SDSafeAsyncMainThread(block);
break;
default:
NSCAssert(NO, @"unexpected policy %tu", self.policy);
Expand Down
3 changes: 3 additions & 0 deletions 3 Tests/Tests/SDUtilsTests.m
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,12 @@ - (void)testSDCallbackQueue {
dispatch_queue_t queue = dispatch_queue_create("testSDCallbackQueue", NULL);
SDCallbackQueue *callbackQueue = [[SDCallbackQueue alloc] initWithDispatchQueue:queue];
__block BOOL called = NO;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[callbackQueue sync:^{
called = YES;
}];
#pragma clang diagnostic pop
expect(called).beTruthy();

__block BOOL called1 = NO;
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.