From 6813f7c156f1e842058549f1801b2eede804f37f Mon Sep 17 00:00:00 2001 From: Dhiogo Brustolin Date: Wed, 24 Apr 2024 12:33:29 +0200 Subject: [PATCH] chore: Rename SentryDispatchQueueWrapper function name for Swift (#3884) 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. --- .../TestSentryDispatchQueueWrapper.swift | 6 ++-- Sources/Sentry/SentryDispatchQueueWrapper.m | 28 +++---------------- Sources/Sentry/SentryUIApplication.m | 22 +++++++++------ .../include/SentryDispatchQueueWrapper.h | 13 ++++----- 4 files changed, 26 insertions(+), 43 deletions(-) diff --git a/SentryTestUtils/TestSentryDispatchQueueWrapper.swift b/SentryTestUtils/TestSentryDispatchQueueWrapper.swift index a200d46c28d..e80c7ba4daf 100644 --- a/SentryTestUtils/TestSentryDispatchQueueWrapper.swift +++ b/SentryTestUtils/TestSentryDispatchQueueWrapper.swift @@ -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() diff --git a/Sources/Sentry/SentryDispatchQueueWrapper.m b/Sources/Sentry/SentryDispatchQueueWrapper.m index b238ee349dd..e092e4f1303 100644 --- a/Sources/Sentry/SentryDispatchQueueWrapper.m +++ b/Sources/Sentry/SentryDispatchQueueWrapper.m @@ -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 diff --git a/Sources/Sentry/SentryUIApplication.m b/Sources/Sentry/SentryUIApplication.m index bd1ca3bbbe1..1a2375f2a80 100644 --- a/Sources/Sentry/SentryUIApplication.m +++ b/Sources/Sentry/SentryUIApplication.m @@ -64,8 +64,9 @@ - (UIApplication *)sharedApplication - (NSArray *)windows { - return [SentryDependencyContainer.sharedInstance.dispatchQueueWrapper - dispatchSyncOnMainQueueWithResult:^id _Nonnull { + __block NSArray *windows = nil; + [SentryDependencyContainer.sharedInstance.dispatchQueueWrapper + dispatchSyncOnMainQueue:^{ UIApplication *app = [self sharedApplication]; NSMutableArray *result = [NSMutableArray array]; @@ -89,9 +90,10 @@ - (UIApplication *)sharedApplication [result addObject:appDelegate.window]; } - return result; + windows = result; } - timeout:0.01]; + timeout:0.01]; + return windows ?: @[]; } - (NSArray *)relevantViewControllers @@ -115,8 +117,10 @@ - (UIApplication *)sharedApplication - (nullable NSArray *)relevantViewControllersNames { - return [SentryDependencyContainer.sharedInstance.dispatchQueueWrapper - dispatchSyncOnMainQueueWithResult:^id _Nonnull { + __block NSArray *result = nil; + + [SentryDependencyContainer.sharedInstance.dispatchQueueWrapper + dispatchSyncOnMainQueue:^{ NSArray *viewControllers = SentryDependencyContainer.sharedInstance.application.relevantViewControllers; NSMutableArray *vcsNames = @@ -124,9 +128,11 @@ - (UIApplication *)sharedApplication 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 *)relevantViewControllerFromWindow:(UIWindow *)window diff --git a/Sources/Sentry/include/SentryDispatchQueueWrapper.h b/Sources/Sentry/include/SentryDispatchQueueWrapper.h index 4a91f36989b..39b65cce61e 100644 --- a/Sources/Sentry/include/SentryDispatchQueueWrapper.h +++ b/Sources/Sentry/include/SentryDispatchQueueWrapper.h @@ -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;