Skip to content

Commit 354b020

Browse files
authored
ref: Don't use functions for notification names (#5566)
1 parent d05d866 commit 354b020

File tree

9 files changed

+62
-119
lines changed

9 files changed

+62
-119
lines changed

SentryTestUtils/TestNSNotificationCenterWrapper.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import Foundation
22
import Sentry
33

4+
#if canImport(UIKit)
5+
public typealias CrossPlatformApplication = UIApplication
6+
#else
7+
public typealias CrossPlatformApplication = NSApplication
8+
#endif
9+
410
@objcMembers public class TestNSNotificationCenterWrapper: SentryNSNotificationCenterWrapper {
511
private enum Observer {
612
case observer(WeakReference<NSObject>, Selector, NSNotification.Name)

Sources/Sentry/SentryAppStateManager.m

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,21 @@ - (instancetype)initWithOptions:(SentryOptions *)options
5050
- (void)start
5151
{
5252
if (self.startCount == 0) {
53-
[self.notificationCenterWrapper
54-
addObserver:self
55-
selector:@selector(didBecomeActive)
56-
name:SentryNSNotificationCenterWrapper.didBecomeActiveNotificationName];
53+
[self.notificationCenterWrapper addObserver:self
54+
selector:@selector(didBecomeActive)
55+
name:SentryDidBecomeActiveNotification];
5756

5857
[self.notificationCenterWrapper addObserver:self
5958
selector:@selector(didBecomeActive)
6059
name:SentryHybridSdkDidBecomeActiveNotificationName];
6160

62-
[self.notificationCenterWrapper
63-
addObserver:self
64-
selector:@selector(willResignActive)
65-
name:SentryNSNotificationCenterWrapper.willResignActiveNotificationName];
61+
[self.notificationCenterWrapper addObserver:self
62+
selector:@selector(willResignActive)
63+
name:SentryWillResignActiveNotification];
6664

67-
[self.notificationCenterWrapper
68-
addObserver:self
69-
selector:@selector(willTerminate)
70-
name:SentryNSNotificationCenterWrapper.willTerminateNotificationName];
65+
[self.notificationCenterWrapper addObserver:self
66+
selector:@selector(willTerminate)
67+
name:SentryWillTerminateNotification];
7168

7269
[self storeCurrentAppState];
7370
}
@@ -99,21 +96,16 @@ - (void)stopWithForce:(BOOL)forceStop
9996
if (self.startCount == 0) {
10097
// Remove the observers with the most specific detail possible, see
10198
// https://developer.apple.com/documentation/foundation/nsnotificationcenter/1413994-removeobserver
102-
[self.notificationCenterWrapper
103-
removeObserver:self
104-
name:SentryNSNotificationCenterWrapper.didBecomeActiveNotificationName];
99+
[self.notificationCenterWrapper removeObserver:self name:SentryDidBecomeActiveNotification];
105100

106101
[self.notificationCenterWrapper
107102
removeObserver:self
108103
name:SentryHybridSdkDidBecomeActiveNotificationName];
109104

110-
[self.notificationCenterWrapper
111-
removeObserver:self
112-
name:SentryNSNotificationCenterWrapper.willResignActiveNotificationName];
105+
[self.notificationCenterWrapper removeObserver:self
106+
name:SentryWillResignActiveNotification];
113107

114-
[self.notificationCenterWrapper
115-
removeObserver:self
116-
name:SentryNSNotificationCenterWrapper.willTerminateNotificationName];
108+
[self.notificationCenterWrapper removeObserver:self name:SentryWillTerminateNotification];
117109
}
118110
}
119111

Sources/Sentry/SentryFramesTracker.m

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,13 @@ - (void)start
136136

137137
_isStarted = YES;
138138

139-
[self.notificationCenter
140-
addObserver:self
141-
selector:@selector(didBecomeActive)
142-
name:SentryNSNotificationCenterWrapper.didBecomeActiveNotificationName];
139+
[self.notificationCenter addObserver:self
140+
selector:@selector(didBecomeActive)
141+
name:SentryDidBecomeActiveNotification];
143142

144-
[self.notificationCenter
145-
addObserver:self
146-
selector:@selector(willResignActive)
147-
name:SentryNSNotificationCenterWrapper.willResignActiveNotificationName];
143+
[self.notificationCenter addObserver:self
144+
selector:@selector(willResignActive)
145+
name:SentryWillResignActiveNotification];
148146

149147
[self unpause];
150148
}
@@ -336,12 +334,8 @@ - (void)stop
336334

337335
// Remove the observers with the most specific detail possible, see
338336
// https://developer.apple.com/documentation/foundation/nsnotificationcenter/1413994-removeobserver
339-
[self.notificationCenter
340-
removeObserver:self
341-
name:SentryNSNotificationCenterWrapper.didBecomeActiveNotificationName];
342-
[self.notificationCenter
343-
removeObserver:self
344-
name:SentryNSNotificationCenterWrapper.willResignActiveNotificationName];
337+
[self.notificationCenter removeObserver:self name:SentryDidBecomeActiveNotification];
338+
[self.notificationCenter removeObserver:self name:SentryWillResignActiveNotification];
345339

346340
@synchronized(self.listeners) {
347341
[self.listeners removeAllObjects];

Sources/Sentry/SentryNSNotificationCenterWrapper.m

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,6 @@
1414

1515
@implementation SentryNSNotificationCenterWrapper
1616

17-
#if SENTRY_HAS_UIKIT
18-
+ (NSNotificationName)didBecomeActiveNotificationName
19-
{
20-
return UIApplicationDidBecomeActiveNotification;
21-
}
22-
23-
+ (NSNotificationName)willResignActiveNotificationName
24-
{
25-
return UIApplicationWillResignActiveNotification;
26-
}
27-
28-
+ (NSNotificationName)willTerminateNotificationName
29-
{
30-
return UIApplicationWillTerminateNotification;
31-
}
32-
33-
+ (NSNotificationName)didEnterBackgroundNotificationName
34-
{
35-
return UIApplicationDidEnterBackgroundNotification;
36-
}
37-
38-
#elif SENTRY_TARGET_MACOS_HAS_UI
39-
+ (NSNotificationName)didBecomeActiveNotificationName
40-
{
41-
return NSApplicationDidBecomeActiveNotification;
42-
}
43-
44-
+ (NSNotificationName)willResignActiveNotificationName
45-
{
46-
return NSApplicationWillResignActiveNotification;
47-
}
48-
49-
+ (NSNotificationName)willTerminateNotificationName
50-
{
51-
return NSApplicationWillTerminateNotification;
52-
}
53-
#endif
54-
5517
- (void)addObserver:(NSObject *)observer
5618
selector:(SEL)aSelector
5719
name:(NSNotificationName)aName

Sources/Sentry/SentrySessionTracker.m

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,20 @@ - (void)start
7272
// ending the cached session.
7373
[self endCachedSession];
7474

75-
[self.notificationCenter
76-
addObserver:self
77-
selector:@selector(didBecomeActive)
78-
name:SentryNSNotificationCenterWrapper.didBecomeActiveNotificationName];
75+
[self.notificationCenter addObserver:self
76+
selector:@selector(didBecomeActive)
77+
name:SentryDidBecomeActiveNotification];
7978

8079
[self.notificationCenter addObserver:self
8180
selector:@selector(didBecomeActive)
8281
name:SentryHybridSdkDidBecomeActiveNotificationName];
83-
[self.notificationCenter
84-
addObserver:self
85-
selector:@selector(willResignActive)
86-
name:SentryNSNotificationCenterWrapper.willResignActiveNotificationName];
82+
[self.notificationCenter addObserver:self
83+
selector:@selector(willResignActive)
84+
name:SentryWillResignActiveNotification];
8785

88-
[self.notificationCenter
89-
addObserver:self
90-
selector:@selector(willTerminate)
91-
name:SentryNSNotificationCenterWrapper.willTerminateNotificationName];
86+
[self.notificationCenter addObserver:self
87+
selector:@selector(willTerminate)
88+
name:SentryWillTerminateNotification];
9289

9390
// Edge case: When starting the SDK after the app did become active, we need to call
9491
// didBecomeActive manually to start the session. This is the case when
@@ -117,17 +114,11 @@ - (void)removeObservers
117114
#if SENTRY_HAS_UIKIT || SENTRY_TARGET_MACOS_HAS_UI
118115
// Remove the observers with the most specific detail possible, see
119116
// https://developer.apple.com/documentation/foundation/nsnotificationcenter/1413994-removeobserver
120-
[self.notificationCenter
121-
removeObserver:self
122-
name:SentryNSNotificationCenterWrapper.didBecomeActiveNotificationName];
117+
[self.notificationCenter removeObserver:self name:SentryDidBecomeActiveNotification];
123118
[self.notificationCenter removeObserver:self
124119
name:SentryHybridSdkDidBecomeActiveNotificationName];
125-
[self.notificationCenter
126-
removeObserver:self
127-
name:SentryNSNotificationCenterWrapper.willResignActiveNotificationName];
128-
[self.notificationCenter
129-
removeObserver:self
130-
name:SentryNSNotificationCenterWrapper.willTerminateNotificationName];
120+
[self.notificationCenter removeObserver:self name:SentryWillResignActiveNotification];
121+
[self.notificationCenter removeObserver:self name:SentryWillTerminateNotification];
131122
#endif
132123
}
133124

Sources/Sentry/include/SentryNSNotificationCenterWrapper.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
#import "SentryDefines.h"
22

3+
#if SENTRY_HAS_UIKIT
4+
# define SentryDidBecomeActiveNotification UIApplicationDidBecomeActiveNotification
5+
# define SentryWillResignActiveNotification UIApplicationWillResignActiveNotification
6+
# define SentryWillTerminateNotification UIApplicationWillTerminateNotification
7+
#elif SENTRY_TARGET_MACOS_HAS_UI
8+
# define SentryDidBecomeActiveNotification NSApplicationDidBecomeActiveNotification
9+
# define SentryWillResignActiveNotification NSApplicationWillResignActiveNotification
10+
# define SentryWillTerminateNotification NSApplicationWillTerminateNotification
11+
#endif
12+
313
NS_ASSUME_NONNULL_BEGIN
414

515
/**
@@ -12,18 +22,6 @@ NS_ASSUME_NONNULL_BEGIN
1222
*/
1323
@interface SentryNSNotificationCenterWrapper : NSObject
1424

15-
#if SENTRY_HAS_UIKIT || SENTRY_TARGET_MACOS_HAS_UI
16-
@property (nonatomic, readonly, copy, class) NSNotificationName didBecomeActiveNotificationName;
17-
@property (nonatomic, readonly, copy, class) NSNotificationName willResignActiveNotificationName;
18-
@property (nonatomic, readonly, copy, class) NSNotificationName willTerminateNotificationName;
19-
#endif
20-
21-
#if SENTRY_HAS_UIKIT
22-
// macOS apps can not enter the background, therefore this notification
23-
// is not available there.
24-
@property (nonatomic, readonly, copy, class) NSNotificationName didEnterBackgroundNotificationName;
25-
#endif
26-
2725
- (void)addObserver:(NSObject *)observer
2826
selector:(SEL)aSelector
2927
name:(NSNotificationName)aName

Tests/SentryTests/Helper/SentryNSNotificationCenterWrapperTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class SentryNSNotificationCenterWrapperTests: XCTestCase {
88
private var didBecomeActiveExpectation: XCTestExpectation!
99
private var willResignActiveExpectation: XCTestExpectation!
1010

11-
private let didBecomeActiveNotification = SentryNSNotificationCenterWrapper.didBecomeActiveNotificationName
12-
private let willResignActiveNotification = SentryNSNotificationCenterWrapper.willResignActiveNotificationName
11+
private let didBecomeActiveNotification = CrossPlatformApplication.didBecomeActiveNotification
12+
private let willResignActiveNotification = CrossPlatformApplication.willResignActiveNotification
1313

1414
override func setUp() {
1515
super.setUp()

Tests/SentryTests/Integrations/Performance/FramesTracking/SentryFramesTrackerTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ class SentryFramesTrackerTests: XCTestCase {
683683
let sut = fixture.sut
684684
sut.start()
685685

686-
fixture.notificationCenter.post(Notification(name: SentryNSNotificationCenterWrapper.willResignActiveNotificationName))
686+
fixture.notificationCenter.post(Notification(name: CrossPlatformApplication.willResignActiveNotification))
687687

688688
XCTAssertFalse(sut.isRunning)
689689
}
@@ -699,9 +699,9 @@ class SentryFramesTrackerTests: XCTestCase {
699699
}
700700
sut.add(listener)
701701

702-
fixture.notificationCenter.post(Notification(name: SentryNSNotificationCenterWrapper.willResignActiveNotificationName))
702+
fixture.notificationCenter.post(Notification(name: CrossPlatformApplication.willResignActiveNotification))
703703

704-
fixture.notificationCenter.post(Notification(name: SentryNSNotificationCenterWrapper.didBecomeActiveNotificationName))
704+
fixture.notificationCenter.post(Notification(name: CrossPlatformApplication.didBecomeActiveNotification))
705705

706706
// Ensure to keep listeners when moving to background
707707
fixture.displayLinkWrapper.normalFrame()

Tests/SentryTests/Integrations/Session/SentrySessionTrackerTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ class SentrySessionTrackerTests: XCTestCase {
608608
fixture.notificationCenter
609609
.post(
610610
Notification(
611-
name: SentryNSNotificationCenterWrapper.didBecomeActiveNotificationName,
611+
name: CrossPlatformApplication.didBecomeActiveNotification,
612612
object: nil,
613613
userInfo: nil
614614
)
@@ -624,7 +624,7 @@ class SentrySessionTrackerTests: XCTestCase {
624624
fixture.notificationCenter
625625
.post(
626626
Notification(
627-
name: SentryNSNotificationCenterWrapper.didEnterBackgroundNotificationName,
627+
name: UIApplication.didEnterBackgroundNotification,
628628
object: nil,
629629
userInfo: nil
630630
)
@@ -652,7 +652,7 @@ class SentrySessionTrackerTests: XCTestCase {
652652
fixture.notificationCenter
653653
.post(
654654
Notification(
655-
name: SentryNSNotificationCenterWrapper.willResignActiveNotificationName,
655+
name: CrossPlatformApplication.willResignActiveNotification,
656656
object: nil,
657657
userInfo: nil
658658
)
@@ -669,7 +669,7 @@ class SentrySessionTrackerTests: XCTestCase {
669669
fixture.notificationCenter
670670
.post(
671671
Notification(
672-
name: SentryNSNotificationCenterWrapper.didBecomeActiveNotificationName,
672+
name: CrossPlatformApplication.didBecomeActiveNotification,
673673
object: nil,
674674
userInfo: nil
675675
)
@@ -699,7 +699,7 @@ class SentrySessionTrackerTests: XCTestCase {
699699
fixture.notificationCenter
700700
.post(
701701
Notification(
702-
name: SentryNSNotificationCenterWrapper.willTerminateNotificationName,
702+
name: CrossPlatformApplication.willTerminateNotification,
703703
object: nil,
704704
userInfo: nil
705705
)
@@ -868,10 +868,10 @@ class SentrySessionTrackerTests: XCTestCase {
868868
XCTAssertEqual(4, notificationNames.count)
869869

870870
XCTAssertEqual([
871-
SentryNSNotificationCenterWrapper.didBecomeActiveNotificationName,
871+
CrossPlatformApplication.didBecomeActiveNotification,
872872
NSNotification.Name(rawValue: SentryHybridSdkDidBecomeActiveNotificationName),
873-
SentryNSNotificationCenterWrapper.willResignActiveNotificationName,
874-
SentryNSNotificationCenterWrapper.willTerminateNotificationName
873+
CrossPlatformApplication.willResignActiveNotification,
874+
CrossPlatformApplication.willTerminateNotification
875875
], notificationNames)
876876
}
877877

0 commit comments

Comments
 (0)