Skip to content

Commit b46bce5

Browse files
committed
ref: Don't use functions for notification names
1 parent 409a607 commit b46bce5

File tree

6 files changed

+57
-114
lines changed

6 files changed

+57
-114
lines changed

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 UIApplicationWillResignActiveNotification
7+
#elif SENTRY_TARGET_MACOS_HAS_UI
8+
# define SentryDidBecomeActiveNotification NSApplicationDidBecomeActiveNotification
9+
# define SentryWillResignActiveNotification NSApplicationWillResignActiveNotification
10+
# define SentryWillTerminateNotification NSApplicationWillResignActiveNotification
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/Integrations/Session/SentrySessionTrackerTests.swift

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
@_spi(Private) import SentryTestUtils
33
import XCTest
44

5+
#if canImport(UIKit)
6+
typealias CrossPlatformApplication = UIApplication
7+
#else
8+
typealias CrossPlatformApplication = NSApplication
9+
#endif
10+
511
class SentrySessionTrackerTests: XCTestCase {
612

713
private static let dsnAsString = TestConstants.dsnAsString(username: "SentrySessionTrackerTests")
@@ -608,7 +614,7 @@ class SentrySessionTrackerTests: XCTestCase {
608614
fixture.notificationCenter
609615
.post(
610616
Notification(
611-
name: SentryNSNotificationCenterWrapper.didBecomeActiveNotificationName,
617+
name: CrossPlatformApplication.didBecomeActiveNotification,
612618
object: nil,
613619
userInfo: nil
614620
)
@@ -624,7 +630,7 @@ class SentrySessionTrackerTests: XCTestCase {
624630
fixture.notificationCenter
625631
.post(
626632
Notification(
627-
name: SentryNSNotificationCenterWrapper.didEnterBackgroundNotificationName,
633+
name: UIApplication.didEnterBackgroundNotification,
628634
object: nil,
629635
userInfo: nil
630636
)
@@ -652,7 +658,7 @@ class SentrySessionTrackerTests: XCTestCase {
652658
fixture.notificationCenter
653659
.post(
654660
Notification(
655-
name: SentryNSNotificationCenterWrapper.willResignActiveNotificationName,
661+
name: CrossPlatformApplication.willResignActiveNotification,
656662
object: nil,
657663
userInfo: nil
658664
)
@@ -669,7 +675,7 @@ class SentrySessionTrackerTests: XCTestCase {
669675
fixture.notificationCenter
670676
.post(
671677
Notification(
672-
name: SentryNSNotificationCenterWrapper.didBecomeActiveNotificationName,
678+
name: CrossPlatformApplication.didBecomeActiveNotification,
673679
object: nil,
674680
userInfo: nil
675681
)
@@ -699,7 +705,7 @@ class SentrySessionTrackerTests: XCTestCase {
699705
fixture.notificationCenter
700706
.post(
701707
Notification(
702-
name: SentryNSNotificationCenterWrapper.willTerminateNotificationName,
708+
name: CrossPlatformApplication.willTerminateNotification,
703709
object: nil,
704710
userInfo: nil
705711
)
@@ -868,10 +874,10 @@ class SentrySessionTrackerTests: XCTestCase {
868874
XCTAssertEqual(4, notificationNames.count)
869875

870876
XCTAssertEqual([
871-
SentryNSNotificationCenterWrapper.didBecomeActiveNotificationName,
877+
CrossPlatformApplication.didBecomeActiveNotification,
872878
NSNotification.Name(rawValue: SentryHybridSdkDidBecomeActiveNotificationName),
873-
SentryNSNotificationCenterWrapper.willResignActiveNotificationName,
874-
SentryNSNotificationCenterWrapper.willTerminateNotificationName
879+
CrossPlatformApplication.willResignActiveNotification,
880+
CrossPlatformApplication.willTerminateNotification
875881
], notificationNames)
876882
}
877883

0 commit comments

Comments
 (0)