Skip to content

Commit

Permalink
fix: implify dispatch queue wrapper access
Browse files Browse the repository at this point in the history
  • Loading branch information
armcknight committed Dec 5, 2023
1 parent 24c239b commit 3139eac
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 34 deletions.
36 changes: 21 additions & 15 deletions Sources/Sentry/SentryTracer.m
Original file line number Diff line number Diff line change
Expand Up @@ -195,32 +195,36 @@ - (void)dispatchIdleTimeout
{
@synchronized(_idleTimeoutLock) {
if (_idleTimeoutBlock != NULL) {
[_configuration.dispatchQueueWrapper dispatchCancel:_idleTimeoutBlock];
[SentryDependencyContainer.sharedInstance.dispatchQueueWrapper
dispatchCancel:_idleTimeoutBlock];
}
__weak SentryTracer *weakSelf = self;
_idleTimeoutBlock = [_configuration.dispatchQueueWrapper createDispatchBlock:^{
if (weakSelf == nil) {
SENTRY_LOG_DEBUG(@"WeakSelf is nil. Not doing anything.");
return;
}
[weakSelf finishInternal];
}];
_idleTimeoutBlock =
[SentryDependencyContainer.sharedInstance.dispatchQueueWrapper createDispatchBlock:^{
if (weakSelf == nil) {
SENTRY_LOG_DEBUG(@"WeakSelf is nil. Not doing anything.");
return;
}
[weakSelf finishInternal];
}];

if (_idleTimeoutBlock == NULL) {
SENTRY_LOG_WARN(@"Couldn't create idle time out block. Can't schedule idle timeout. "
@"Finishing transaction");
// If the transaction has no children, the SDK will discard it.
[self finishInternal];
} else {
[_configuration.dispatchQueueWrapper dispatchAfter:_configuration.idleTimeout
block:_idleTimeoutBlock];
[SentryDependencyContainer.sharedInstance.dispatchQueueWrapper
dispatchAfter:_configuration.idleTimeout
block:_idleTimeoutBlock];
}
}
}

- (BOOL)hasIdleTimeout
{
return _configuration.idleTimeout > 0 && _configuration.dispatchQueueWrapper != nil;
return _configuration.idleTimeout > 0
&& SentryDependencyContainer.sharedInstance.dispatchQueueWrapper != nil;
}

- (BOOL)isAutoGeneratedTransaction
Expand All @@ -232,15 +236,16 @@ - (void)cancelIdleTimeout
{
@synchronized(_idleTimeoutLock) {
if ([self hasIdleTimeout]) {
[_configuration.dispatchQueueWrapper dispatchCancel:_idleTimeoutBlock];
[SentryDependencyContainer.sharedInstance.dispatchQueueWrapper
dispatchCancel:_idleTimeoutBlock];
}
}
}

- (void)startDeadlineTimer
{
__weak SentryTracer *weakSelf = self;
[_configuration.dispatchQueueWrapper dispatchOnMainQueue:^{
[SentryDependencyContainer.sharedInstance.dispatchQueueWrapper dispatchOnMainQueue:^{
weakSelf.deadlineTimer = [weakSelf.configuration.timerFactory
scheduledTimerWithTimeInterval:SENTRY_AUTO_TRANSACTION_DEADLINE
repeats:NO
Expand Down Expand Up @@ -282,7 +287,7 @@ - (void)cancelDeadlineTimer

// The timer must be invalidated from the thread on which the timer was installed, see
// https://developer.apple.com/documentation/foundation/nstimer/1415405-invalidate#1770468
[_configuration.dispatchQueueWrapper dispatchOnMainQueue:^{
[SentryDependencyContainer.sharedInstance.dispatchQueueWrapper dispatchOnMainQueue:^{
if (weakSelf == nil) {
SENTRY_LOG_DEBUG(@"WeakSelf is nil. Not invalidating deadlineTimer.");
return;
Expand Down Expand Up @@ -441,7 +446,8 @@ - (void)canBeFinished
}

if (!self.wasFinishCalled || hasUnfinishedChildSpansToWaitFor) {
SENTRY_LOG_DEBUG(@"Span with id %@ has children but isn't waiting for them right now.",
SENTRY_LOG_DEBUG(@"Span with id %@ has children but hasn't finished yet so isn't waiting "
@"for them right now.",
self.spanId.sentrySpanIdString);
return;
}
Expand Down
6 changes: 1 addition & 5 deletions Sources/Sentry/SentryUIEventTrackerTransactionMode.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,16 @@
@interface
SentryUIEventTrackerTransactionMode ()

@property (nonatomic, strong) SentryDispatchQueueWrapper *dispatchQueueWrapper;
@property (nonatomic, assign) NSTimeInterval idleTimeout;
@property (nullable, nonatomic, strong) NSMutableArray<SentryTracer *> *activeTransactions;

@end

@implementation SentryUIEventTrackerTransactionMode

- (instancetype)initWithDispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper
idleTimeout:(NSTimeInterval)idleTimeout
- (instancetype)initWithIdleTimeout:(NSTimeInterval)idleTimeout
{
if (self = [super init]) {
self.dispatchQueueWrapper = dispatchQueueWrapper;
self.idleTimeout = idleTimeout;
self.activeTransactions = [NSMutableArray new];
}
Expand Down Expand Up @@ -91,7 +88,6 @@ - (void)handleUIEvent:(NSString *)action
SentryTracerConfiguration *config) {
config.idleTimeout = self.idleTimeout;
config.waitForChildren = YES;
config.dispatchQueueWrapper = self.dispatchQueueWrapper;
}]];

SENTRY_LOG_DEBUG(@"Automatically started a new transaction with name: "
Expand Down
7 changes: 2 additions & 5 deletions Sources/Sentry/SentryUIEventTrackingIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#if SENTRY_HAS_UIKIT

# import <SentryDependencyContainer.h>
# import <SentryLog.h>
# import <SentryNSDataSwizzling.h>
# import <SentryOptions+Private.h>
Expand All @@ -25,10 +24,8 @@ - (BOOL)installWithOptions:(SentryOptions *)options
return NO;
}

SentryDependencyContainer *dependencies = [SentryDependencyContainer sharedInstance];
SentryUIEventTrackerTransactionMode *mode = [[SentryUIEventTrackerTransactionMode alloc]
initWithDispatchQueueWrapper:dependencies.dispatchQueueWrapper
idleTimeout:options.idleTimeout];
SentryUIEventTrackerTransactionMode *mode =
[[SentryUIEventTrackerTransactionMode alloc] initWithIdleTimeout:options.idleTimeout];

self.uiEventTracker = [[SentryUIEventTracker alloc] initWithMode:mode];

Expand Down
5 changes: 0 additions & 5 deletions Sources/Sentry/include/SentryTracerConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic) BOOL waitForChildren;

/**
* A dispatch queue wrapper to intermediate between the tracer and dispatch calls.
*/
@property (nonatomic, strong, nullable) SentryDispatchQueueWrapper *dispatchQueueWrapper;

#if SENTRY_TARGET_PROFILING_SUPPORTED
/**
* Whether to sample a profile corresponding to this transaction
Expand Down
5 changes: 1 addition & 4 deletions Sources/Sentry/include/SentryUIEventTrackerTransactionMode.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

#if SENTRY_HAS_UIKIT

@class SentryDispatchQueueWrapper;

NS_ASSUME_NONNULL_BEGIN

@interface SentryUIEventTrackerTransactionMode : NSObject <SentryUIEventTrackerMode>
SENTRY_NO_INIT

- (instancetype)initWithDispatchQueueWrapper:(SentryDispatchQueueWrapper *)dispatchQueueWrapper
idleTimeout:(NSTimeInterval)idleTimeout;
- (instancetype)initWithIdleTimeout:(NSTimeInterval)idleTimeout;

@end

Expand Down

0 comments on commit 3139eac

Please sign in to comment.