Skip to content

Commit 1b008ee

Browse files
authored
ref: Convert SentryNSTimerFactory to Swift (#6015)
* ref: Convert `SentryNSTimerFactory` to Swift * Fix build with SPM * Print sentry error log if not on main thread * Remove import * Use `SentryProfilingSwiftHelpers` instead of a new class
1 parent d157d83 commit 1b008ee

15 files changed

+63
-89
lines changed

Sentry.xcodeproj/project.pbxproj

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,6 @@
660660
844EDC70294143B900C86F34 /* SentryNSProcessInfoWrapper.mm in Sources */ = {isa = PBXBuildFile; fileRef = 844EDC6E294143B900C86F34 /* SentryNSProcessInfoWrapper.mm */; };
661661
844EDC76294144DB00C86F34 /* SentrySystemWrapper.h in Headers */ = {isa = PBXBuildFile; fileRef = 844EDC74294144DB00C86F34 /* SentrySystemWrapper.h */; };
662662
844EDC77294144DB00C86F34 /* SentrySystemWrapper.mm in Sources */ = {isa = PBXBuildFile; fileRef = 844EDC75294144DB00C86F34 /* SentrySystemWrapper.mm */; };
663-
844EDCE52947DC3100C86F34 /* SentryNSTimerFactory.h in Headers */ = {isa = PBXBuildFile; fileRef = 844EDCE32947DC3100C86F34 /* SentryNSTimerFactory.h */; };
664-
844EDCE62947DC3100C86F34 /* SentryNSTimerFactory.m in Sources */ = {isa = PBXBuildFile; fileRef = 844EDCE42947DC3100C86F34 /* SentryNSTimerFactory.m */; };
665663
844EDD6C2949387000C86F34 /* SentryMetricProfiler.h in Headers */ = {isa = PBXBuildFile; fileRef = 844EDD6B2949387000C86F34 /* SentryMetricProfiler.h */; };
666664
8453421228BE855D00C22EEC /* SentrySampleDecision.m in Sources */ = {isa = PBXBuildFile; fileRef = 8453421128BE855D00C22EEC /* SentrySampleDecision.m */; };
667665
8453421628BE8A9500C22EEC /* SentrySpanStatus.m in Sources */ = {isa = PBXBuildFile; fileRef = 8453421528BE8A9500C22EEC /* SentrySpanStatus.m */; };
@@ -1040,6 +1038,7 @@
10401038
F458D1172E186DF20028273E /* SentryScopePersistentStore+Fingerprint.swift in Sources */ = {isa = PBXBuildFile; fileRef = F458D1162E186DF20028273E /* SentryScopePersistentStore+Fingerprint.swift */; };
10411039
F458D1192E186E000028273E /* SentryScopePersistentStore+Extras.swift in Sources */ = {isa = PBXBuildFile; fileRef = F458D1182E186E000028273E /* SentryScopePersistentStore+Extras.swift */; };
10421040
F46DA6C32E1DBCA000DF6E3B /* SentryScopePersistentStore+Helper.swift in Sources */ = {isa = PBXBuildFile; fileRef = F46DA6C22E1DBCA000DF6E3B /* SentryScopePersistentStore+Helper.swift */; };
1041+
F48F767C2E60B555009D4E7D /* SentryNSTimerFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = F48F767B2E60B555009D4E7D /* SentryNSTimerFactory.swift */; };
10431042
F49D41982DEA27AF00D9244E /* SentryUseNSExceptionCallstackWrapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F49D41972DEA27AF00D9244E /* SentryUseNSExceptionCallstackWrapperTests.swift */; };
10441043
F49D419A2DEA2FB000D9244E /* SentryCrashExceptionApplicationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F49D41992DEA2FB000D9244E /* SentryCrashExceptionApplicationTests.swift */; };
10451044
F49D419C2DEA30C300D9244E /* SentryCrashExceptionApplicationHelper.h in Headers */ = {isa = PBXBuildFile; fileRef = F49D419B2DEA30B800D9244E /* SentryCrashExceptionApplicationHelper.h */; };
@@ -1964,8 +1963,6 @@
19641963
844EDC74294144DB00C86F34 /* SentrySystemWrapper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentrySystemWrapper.h; path = include/SentrySystemWrapper.h; sourceTree = "<group>"; };
19651964
844EDC75294144DB00C86F34 /* SentrySystemWrapper.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = SentrySystemWrapper.mm; sourceTree = "<group>"; };
19661965
844EDC7829415AB300C86F34 /* TestSentrySystemWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestSentrySystemWrapper.swift; sourceTree = "<group>"; };
1967-
844EDCE32947DC3100C86F34 /* SentryNSTimerFactory.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryNSTimerFactory.h; path = include/SentryNSTimerFactory.h; sourceTree = "<group>"; };
1968-
844EDCE42947DC3100C86F34 /* SentryNSTimerFactory.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryNSTimerFactory.m; sourceTree = "<group>"; };
19691966
844EDCE72947DCD700C86F34 /* TestSentryNSTimerFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestSentryNSTimerFactory.swift; sourceTree = "<group>"; };
19701967
844EDD6B2949387000C86F34 /* SentryMetricProfiler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SentryMetricProfiler.h; path = Sources/Sentry/include/SentryMetricProfiler.h; sourceTree = SOURCE_ROOT; };
19711968
8452E77D2DBC43CF0087020B /* Brewfile-ci-build */ = {isa = PBXFileReference; lastKnownFileType = text; path = "Brewfile-ci-build"; sourceTree = "<group>"; };
@@ -2385,6 +2382,7 @@
23852382
F458D1162E186DF20028273E /* SentryScopePersistentStore+Fingerprint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SentryScopePersistentStore+Fingerprint.swift"; sourceTree = "<group>"; };
23862383
F458D1182E186E000028273E /* SentryScopePersistentStore+Extras.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SentryScopePersistentStore+Extras.swift"; sourceTree = "<group>"; };
23872384
F46DA6C22E1DBCA000DF6E3B /* SentryScopePersistentStore+Helper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SentryScopePersistentStore+Helper.swift"; sourceTree = "<group>"; };
2385+
F48F767B2E60B555009D4E7D /* SentryNSTimerFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryNSTimerFactory.swift; sourceTree = "<group>"; };
23882386
F49D41972DEA27AF00D9244E /* SentryUseNSExceptionCallstackWrapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryUseNSExceptionCallstackWrapperTests.swift; sourceTree = "<group>"; };
23892387
F49D41992DEA2FB000D9244E /* SentryCrashExceptionApplicationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryCrashExceptionApplicationTests.swift; sourceTree = "<group>"; };
23902388
F49D419B2DEA30B800D9244E /* SentryCrashExceptionApplicationHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SentryCrashExceptionApplicationHelper.h; path = include/SentryCrashExceptionApplicationHelper.h; sourceTree = "<group>"; };
@@ -2600,6 +2598,7 @@
26002598
621D9F2D2B9B030E003D94DE /* Helper */ = {
26012599
isa = PBXGroup;
26022600
children = (
2601+
F48F767B2E60B555009D4E7D /* SentryNSTimerFactory.swift */,
26032602
FABE8E162E307A7C0040809A /* Dependencies.swift */,
26042603
FABE8E142E307A5C0040809A /* SentrySDK.swift */,
26052604
FA3A42712E1C5F9B00A08C39 /* SentryNSNotificationCenterWrapper.swift */,
@@ -2943,8 +2942,6 @@
29432942
844EDC6E294143B900C86F34 /* SentryNSProcessInfoWrapper.mm */,
29442943
844EDC74294144DB00C86F34 /* SentrySystemWrapper.h */,
29452944
844EDC75294144DB00C86F34 /* SentrySystemWrapper.mm */,
2946-
844EDCE32947DC3100C86F34 /* SentryNSTimerFactory.h */,
2947-
844EDCE42947DC3100C86F34 /* SentryNSTimerFactory.m */,
29482945
FAB359972E05D7E90083D5E3 /* SentryEventSwiftHelper.h */,
29492946
FAB359992E05D8080083D5E3 /* SentryEventSwiftHelper.m */,
29502947
33042A0B29DAF5F400C60085 /* SentryExtraContextProvider.h */,
@@ -5121,7 +5118,6 @@
51215118
7BD86EC5264A63F6005439DB /* SentrySysctl.h in Headers */,
51225119
63BE85701ECEC6DE00DC44F5 /* SentryDateUtils.h in Headers */,
51235120
63FE709520DA4C1000CDBAE8 /* SentryCrashReportFilterBasic.h in Headers */,
5124-
844EDCE52947DC3100C86F34 /* SentryNSTimerFactory.h in Headers */,
51255121
D8B088B629C9E3FF00213258 /* SentryTracerConfiguration.h in Headers */,
51265122
7B63459D280EBA6300CFA05A /* SentryUIEventTracker.h in Headers */,
51275123
7B7D873424864C6600D2ECFF /* SentryCrashDefaultMachineContextWrapper.h in Headers */,
@@ -5799,6 +5795,7 @@
57995795
FA67DD192DDBD4EA00896B02 /* SwizzleClassNameExclude.swift in Sources */,
58005796
7B2A70DD27D6083D008B0D15 /* SentryThreadWrapper.m in Sources */,
58015797
D8ACE3C72762187200F5A213 /* SentryNSDataSwizzling.m in Sources */,
5798+
F48F767C2E60B555009D4E7D /* SentryNSTimerFactory.swift in Sources */,
58025799
638DC9A11EBC6B6400A66E41 /* SentryRequestOperation.m in Sources */,
58035800
63AA767A1EB8D20500D153DE /* SentryLogC.m in Sources */,
58045801
84B0DFF42CD2CF64007FB332 /* SentryUserFeedbackFormController.swift in Sources */,
@@ -5811,7 +5808,6 @@
58115808
D4ECA4022E3CBEDE00C757EA /* SentryDummyPrivateEmptyClass.m in Sources */,
58125809
D80299502BA83A88000F0081 /* SentryPixelBuffer.swift in Sources */,
58135810
15E0A8F22411A45A00F044E3 /* SentrySession.m in Sources */,
5814-
844EDCE62947DC3100C86F34 /* SentryNSTimerFactory.m in Sources */,
58155811
D83D079C2B7F9D1C00CC9674 /* SentryMsgPackSerializer.m in Sources */,
58165812
D452FCBF2DDB6FD200AFF56F /* SentryWatchdogTerminationAttributesProcessor.swift in Sources */,
58175813
7B6D1261265F784000C9BE4B /* PrivateSentrySDKOnly.m in Sources */,

SentryTestUtils/SentryTestUtils-ObjC-BridgingHeader.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
#import "SentryHub+Test.h"
4545
#import "SentryLogC.h"
4646
#import "SentryNSProcessInfoWrapper.h"
47-
#import "SentryNSTimerFactory.h"
4847
#import "SentryNetworkTracker.h"
4948
#import "SentryPerformanceTracker+Testing.h"
5049
#import "SentryReachability.h"

SentryTestUtils/TestSentryNSTimerFactory.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Foundation
33

44
// We must not subclass NSTimer, see https://developer.apple.com/documentation/foundation/nstimer#1770465.
55
// Therefore we return a NSTimer instance here with TimeInterval.infinity.
6-
public class TestSentryNSTimerFactory: SentryNSTimerFactory {
6+
@_spi(Private) public class TestSentryNSTimerFactory: SentryNSTimerFactory {
77
public struct Overrides {
88
private var _interval: TimeInterval?
99

@@ -44,17 +44,16 @@ public class TestSentryNSTimerFactory: SentryNSTimerFactory {
4444
self.currentDateProvider = currentDateProvider
4545
super.init()
4646
}
47-
}
4847

49-
// MARK: Superclass overrides
50-
public extension TestSentryNSTimerFactory {
51-
override func scheduledTimer(withTimeInterval interval: TimeInterval, repeats: Bool, block: @escaping (Timer) -> Void) -> Timer {
48+
@objc
49+
public override func scheduledTimer(withTimeInterval interval: TimeInterval, repeats: Bool, block: @escaping (Timer) -> Void) -> Timer {
5250
let timer = Timer.scheduledTimer(withTimeInterval: TimeInterval.infinity, repeats: repeats, block: block)
5351
overrides = Overrides(timer: timer, interval: interval, block: block, lastFireDate: currentDateProvider.date())
5452
return timer
5553
}
5654

57-
override func scheduledTimer(withTimeInterval ti: TimeInterval, target aTarget: Any, selector aSelector: Selector, userInfo: Any?, repeats yesOrNo: Bool) -> Timer {
55+
@objc
56+
public override func scheduledTimer(withTimeInterval ti: TimeInterval, target aTarget: Any, selector aSelector: Selector, userInfo: Any?, repeats yesOrNo: Bool) -> Timer {
5857
let timer = Timer.scheduledTimer(timeInterval: ti, target: aTarget, selector: aSelector, userInfo: userInfo, repeats: yesOrNo)
5958
//swiftlint:disable force_cast
6059
let invocationInfo = Overrides.InvocationInfo(target: aTarget as! NSObject, selector: aSelector)

Sources/Sentry/Profiling/SentryContinuousProfiler.mm

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
# import "SentryDependencyContainer.h"
66
# import "SentryLogC.h"
77
# import "SentryMetricProfiler.h"
8-
# import "SentryNSTimerFactory.h"
98
# import "SentryProfiler+Private.h"
109
# import "SentryProfilerSerialization.h"
1110
# import "SentryProfilerState.h"
@@ -193,12 +192,8 @@ + (void)scheduleTimer
193192
return;
194193
}
195194

196-
_chunkTimer = [SentryDependencyContainer.sharedInstance.timerFactory
197-
scheduledTimerWithTimeInterval:kSentryProfilerChunkExpirationInterval
198-
target:self
199-
selector:@selector(timerExpired)
200-
userInfo:nil
201-
repeats:YES];
195+
_chunkTimer = sentry_scheduledTimerWithTarget(
196+
kSentryProfilerChunkExpirationInterval, self, @selector(timerExpired), nil, YES);
202197
});
203198
}
204199

Sources/Sentry/Profiling/SentryProfilingSwiftHelpers.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,25 @@
146146
usingBlock:^(NSNotification *_Nonnull notification) { block(); }];
147147
}
148148

149+
NSTimer *
150+
sentry_scheduledTimer(NSTimeInterval interval, BOOL repeats, dispatch_block_t block)
151+
{
152+
return [SentryDependencyContainer.sharedInstance.timerFactory
153+
scheduledTimerWithTimeInterval:interval
154+
repeats:repeats
155+
block:^(NSTimer *_Nonnull timer) { block(); }];
156+
}
157+
158+
NSTimer *
159+
sentry_scheduledTimerWithTarget(
160+
NSTimeInterval interval, id target, SEL selector, _Nullable id userInfo, BOOL repeats)
161+
{
162+
return [SentryDependencyContainer.sharedInstance.timerFactory
163+
scheduledTimerWithTimeInterval:interval
164+
target:target
165+
selector:selector
166+
userInfo:userInfo
167+
repeats:repeats];
168+
}
169+
149170
#endif // SENTRY_TARGET_PROFILING_SUPPORTED

Sources/Sentry/Profiling/SentryTraceProfiler.mm

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
# import "SentryLogC.h"
88
# import "SentryMetricProfiler.h"
9-
# import "SentryNSTimerFactory.h"
109
# import "SentryProfiledTracerConcurrency.h"
1110
# import "SentryProfiler+Private.h"
1211
# import "SentryProfilingSwiftHelpers.h"
@@ -88,13 +87,8 @@ + (void)scheduleTimeoutTimer
8887
return;
8988
}
9089

91-
_sentry_threadUnsafe_traceProfileTimeoutTimer =
92-
[SentryDependencyContainer.sharedInstance.timerFactory
93-
scheduledTimerWithTimeInterval:kSentryProfilerTimeoutInterval
94-
repeats:NO
95-
block:^(NSTimer *_Nonnull timer) {
96-
[self timeoutTimerExpired];
97-
}];
90+
_sentry_threadUnsafe_traceProfileTimeoutTimer = sentry_scheduledTimer(
91+
kSentryProfilerTimeoutInterval, NO, ^{ [self timeoutTimerExpired]; });
9892
});
9993
}
10094

Sources/Sentry/SentryDependencyContainer.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#import "SentryInternalDefines.h"
1313
#import "SentryLogC.h"
1414
#import "SentryNSProcessInfoWrapper.h"
15-
#import "SentryNSTimerFactory.h"
1615
#import "SentryOptions+Private.h"
1716
#import "SentryRandom.h"
1817
#import "SentrySDK+Private.h"

Sources/Sentry/SentryHub.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#import "SentryInternalDefines.h"
1313
#import "SentryLevelMapper.h"
1414
#import "SentryLogC.h"
15-
#import "SentryNSTimerFactory.h"
1615
#import "SentryOptions+Private.h"
1716
#import "SentryPerformanceTracker.h"
1817
#import "SentryProfilingConditionals.h"

Sources/Sentry/SentryMetricProfiler.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
# import "SentryEvent+Private.h"
99
# import "SentryFormatter.h"
1010
# import "SentryLogC.h"
11-
# import "SentryNSTimerFactory.h"
1211
# import "SentrySwift.h"
1312
# import "SentrySystemWrapper.h"
1413
# import "SentryTime.h"

Sources/Sentry/SentryNSTimerFactory.m

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)