Skip to content

Commit c184125

Browse files
authored
Merge pull request SDWebImage#3797 from dreampiggy/feature/remove_asyncMainQueue
Change the default callback queue policy to SafeAsyncMainThread, don't need main queue check at all
2 parents ac3a61b + ee319fc commit c184125

File tree

3 files changed

+10
-28
lines changed

3 files changed

+10
-28
lines changed

SDWebImage/Core/SDCallbackQueue.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,16 @@ typedef NS_ENUM(NSUInteger, SDCallbackPolicy) {
1717
SDCallbackPolicyDispatch = 1,
1818
/// Ignore any async/sync and just directly invoke `block` in current queue (without `dispatch_async`/`dispatch_sync`)
1919
SDCallbackPolicyInvoke = 2,
20-
/// 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
21-
SDCallbackPolicySafeAsyncMainQueue = 3,
2220
/// 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
23-
SDCallbackPolicySafeAsyncMainThread = 4,
21+
SDCallbackPolicySafeAsyncMainThread = 3,
2422
};
2523

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

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

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

5351
/// Submits a block for execution and returns after that block finishes executing.
5452
/// - Parameter block: The block that contains the work to perform.
55-
- (void)sync:(nonnull dispatch_block_t)block;
53+
- (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));;
5654

5755
/// Schedules a block asynchronously for execution.
5856
/// - Parameter block: The block that contains the work to perform.

SDWebImage/Core/SDCallbackQueue.m

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,7 @@ @interface SDCallbackQueue ()
1515

1616
@end
1717

18-
static inline void SDSafeMainQueueAsync(dispatch_block_t _Nonnull block) {
19-
if (NSThread.isMainThread) {
20-
// Match exists `dispatch_main_async_safe` behavior
21-
const char *currentLabel = dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL);
22-
const char *mainLabel = dispatch_queue_get_label(dispatch_get_main_queue());
23-
if (mainLabel == currentLabel) {
24-
block();
25-
return;
26-
}
27-
}
28-
dispatch_async(dispatch_get_main_queue(), block);
29-
}
30-
31-
static inline void SDSafeMainThreadAsync(dispatch_block_t _Nonnull block) {
18+
static inline void SDSafeAsyncMainThread(dispatch_block_t _Nonnull block) {
3219
if (NSThread.isMainThread) {
3320
block();
3421
} else {
@@ -71,7 +58,7 @@ - (instancetype)initWithDispatchQueue:(dispatch_queue_t)queue {
7158

7259
+ (SDCallbackQueue *)mainQueue {
7360
SDCallbackQueue *queue = [[SDCallbackQueue alloc] initWithDispatchQueue:dispatch_get_main_queue()];
74-
queue->_policy = SDCallbackPolicySafeAsyncMainQueue;
61+
queue->_policy = SDCallbackPolicySafeAsyncMainThread;
7562
return queue;
7663
}
7764

@@ -99,11 +86,8 @@ - (void)sync:(nonnull dispatch_block_t)block {
9986
case SDCallbackPolicyInvoke:
10087
block();
10188
break;
102-
case SDCallbackPolicySafeAsyncMainQueue:
103-
SDSafeMainQueueAsync(block);
104-
break;
10589
case SDCallbackPolicySafeAsyncMainThread:
106-
SDSafeMainThreadAsync(block);
90+
SDSafeAsyncMainThread(block);
10791
break;
10892
default:
10993
NSCAssert(NO, @"unexpected policy %tu", self.policy);
@@ -122,11 +106,8 @@ - (void)async:(nonnull dispatch_block_t)block {
122106
case SDCallbackPolicyInvoke:
123107
block();
124108
break;
125-
case SDCallbackPolicySafeAsyncMainQueue:
126-
SDSafeMainQueueAsync(block);
127-
break;
128109
case SDCallbackPolicySafeAsyncMainThread:
129-
SDSafeMainThreadAsync(block);
110+
SDSafeAsyncMainThread(block);
130111
break;
131112
default:
132113
NSCAssert(NO, @"unexpected policy %tu", self.policy);

Tests/Tests/SDUtilsTests.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,12 @@ - (void)testSDCallbackQueue {
171171
dispatch_queue_t queue = dispatch_queue_create("testSDCallbackQueue", NULL);
172172
SDCallbackQueue *callbackQueue = [[SDCallbackQueue alloc] initWithDispatchQueue:queue];
173173
__block BOOL called = NO;
174+
#pragma clang diagnostic push
175+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
174176
[callbackQueue sync:^{
175177
called = YES;
176178
}];
179+
#pragma clang diagnostic pop
177180
expect(called).beTruthy();
178181

179182
__block BOOL called1 = NO;

0 commit comments

Comments
 (0)