Skip to content

Commit

Permalink
chore: Rename SentryDispatchQueueWrapper function name for Swift (#3884)
Browse files Browse the repository at this point in the history
SentryDispatchQueueWrapper is confusing to use with Swift. It behaves as if it were a DispatchQueue but also includes functions that interact with the main queue. When converting to Swift, the functions that manipulate its own queue overlap with those that interact with the main queue, distinguished only by the argument name, which can be omitted because it's a block.

Renaming those functions in Swift would better reflect their respective actions.
  • Loading branch information
brustolin authored Apr 24, 2024
1 parent 135dec6 commit 6813f7c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 43 deletions.
6 changes: 3 additions & 3 deletions SentryTestUtils/TestSentryDispatchQueueWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ public class TestSentryDispatchQueueWrapper: SentryDispatchQueueWrapper {
public var blockOnMainInvocations = Invocations<() -> Void>()
public var blockBeforeMainBlock: () -> Bool = { true }

public override func dispatch(onMainQueue block: @escaping () -> Void) {
public override func dispatchOnMainQueue(block: @escaping () -> Void) {
blockOnMainInvocations.record(block)
if blockBeforeMainBlock() {
block()
}
}

public override func dispatchAsync(onMainQueue block: @escaping () -> Void) {
public override func dispatchAsyncOnMainQueue(block: @escaping () -> Void) {
blockOnMainInvocations.record(block)
if blockBeforeMainBlock() {
block()
}
}

public override func dispatchSync(onMainQueue block: @escaping () -> Void) {
public override func dispatchSyncOnMainQueue(block: @escaping () -> Void) {
blockOnMainInvocations.record(block)
if blockBeforeMainBlock() {
block()
Expand Down
28 changes: 4 additions & 24 deletions Sources/Sentry/SentryDispatchQueueWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,43 +56,23 @@ - (void)dispatchSyncOnMainQueue:(void (^)(void))block
}
}

- (nullable id)dispatchSyncOnMainQueueWithResult:(id (^)(void))block
{
return [self dispatchSyncOnMainQueueWithResult:block timeout:DISPATCH_TIME_FOREVER];
}

- (BOOL)dispatchSyncOnMainQueue:(void (^)(void))block timeout:(NSTimeInterval)timeout
{
NSNumber *result = [self
dispatchSyncOnMainQueueWithResult:^id _Nonnull {
block();
return @YES;
}
timeout:timeout];
return result.boolValue;
}

- (nullable id)dispatchSyncOnMainQueueWithResult:(id (^)(void))block timeout:(NSTimeInterval)timeout
{
if ([NSThread isMainThread]) {
return block();
block();
} else {
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block id result;

dispatch_async(dispatch_get_main_queue(), ^{
result = block();
block();
dispatch_semaphore_signal(semaphore);
});

dispatch_time_t timeout_t
= dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC));
if (dispatch_semaphore_wait(semaphore, timeout_t) == 0) {
return result;
} else {
return nil;
}
return dispatch_semaphore_wait(semaphore, timeout_t) == 0;
}
return YES;
}

- (void)dispatchAfter:(NSTimeInterval)interval block:(dispatch_block_t)block
Expand Down
22 changes: 14 additions & 8 deletions Sources/Sentry/SentryUIApplication.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ - (UIApplication *)sharedApplication

- (NSArray<UIWindow *> *)windows
{
return [SentryDependencyContainer.sharedInstance.dispatchQueueWrapper
dispatchSyncOnMainQueueWithResult:^id _Nonnull {
__block NSArray<UIWindow *> *windows = nil;
[SentryDependencyContainer.sharedInstance.dispatchQueueWrapper
dispatchSyncOnMainQueue:^{
UIApplication *app = [self sharedApplication];
NSMutableArray *result = [NSMutableArray array];

Expand All @@ -89,9 +90,10 @@ - (UIApplication *)sharedApplication
[result addObject:appDelegate.window];
}

return result;
windows = result;
}
timeout:0.01];
timeout:0.01];
return windows ?: @[];
}

- (NSArray<UIViewController *> *)relevantViewControllers
Expand All @@ -115,18 +117,22 @@ - (UIApplication *)sharedApplication

- (nullable NSArray<NSString *> *)relevantViewControllersNames
{
return [SentryDependencyContainer.sharedInstance.dispatchQueueWrapper
dispatchSyncOnMainQueueWithResult:^id _Nonnull {
__block NSArray<NSString *> *result = nil;

[SentryDependencyContainer.sharedInstance.dispatchQueueWrapper
dispatchSyncOnMainQueue:^{
NSArray *viewControllers
= SentryDependencyContainer.sharedInstance.application.relevantViewControllers;
NSMutableArray *vcsNames =
[[NSMutableArray alloc] initWithCapacity:viewControllers.count];
for (id vc in viewControllers) {
[vcsNames addObject:[SwiftDescriptor getObjectClassName:vc]];
}
return [NSArray arrayWithArray:vcsNames];
result = [NSArray arrayWithArray:vcsNames];
}
timeout:0.01];
timeout:0.01];

return result;
}

- (NSArray<UIViewController *> *)relevantViewControllerFromWindow:(UIWindow *)window
Expand Down
13 changes: 5 additions & 8 deletions Sources/Sentry/include/SentryDispatchQueueWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@ NS_ASSUME_NONNULL_BEGIN

- (void)dispatchAsyncWithBlock:(void (^)(void))block;

- (void)dispatchAsyncOnMainQueue:(void (^)(void))block;
- (void)dispatchAsyncOnMainQueue:(void (^)(void))block
NS_SWIFT_NAME(dispatchAsyncOnMainQueue(block:));

- (void)dispatchOnMainQueue:(void (^)(void))block;
- (void)dispatchOnMainQueue:(void (^)(void))block NS_SWIFT_NAME(dispatchOnMainQueue(block:));

- (void)dispatchSyncOnMainQueue:(void (^)(void))block;

- (nullable id)dispatchSyncOnMainQueueWithResult:(id (^)(void))block;
- (void)dispatchSyncOnMainQueue:(void (^)(void))block
NS_SWIFT_NAME(dispatchSyncOnMainQueue(block:));

- (BOOL)dispatchSyncOnMainQueue:(void (^)(void))block timeout:(NSTimeInterval)timeout;

- (nullable id)dispatchSyncOnMainQueueWithResult:(id (^)(void))block
timeout:(NSTimeInterval)timeout;

- (void)dispatchAfter:(NSTimeInterval)interval block:(dispatch_block_t)block;

- (void)dispatchCancel:(dispatch_block_t)block;
Expand Down

0 comments on commit 6813f7c

Please sign in to comment.