Skip to content

Commit 2959901

Browse files
committed
Create class OSCopyOnWriteSet
1 parent fc4c1d3 commit 2959901

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@
9090
3C47A975292642B100312125 /* OneSignalConfigManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 3C47A973292642B100312125 /* OneSignalConfigManager.m */; };
9191
3C4F9E4428A4466C009F453A /* OSOperationRepo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C4F9E4328A4466C009F453A /* OSOperationRepo.swift */; };
9292
3C5117172B15C31E00563465 /* OSUserState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C5117162B15C31E00563465 /* OSUserState.swift */; };
93+
3C5501402E09CF0100E77DF7 /* OSCopyOnWriteSet.h in Headers */ = {isa = PBXBuildFile; fileRef = 3C55013E2E09CF0100E77DF7 /* OSCopyOnWriteSet.h */; };
94+
3C5501412E09CF0100E77DF7 /* OSCopyOnWriteSet.m in Sources */ = {isa = PBXBuildFile; fileRef = 3C55013F2E09CF0100E77DF7 /* OSCopyOnWriteSet.m */; };
9395
3C5929E32CAD9EC50020D6FF /* OneSignalUserManagerImpl+OSLoggable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C5929E22CAD9EC50020D6FF /* OneSignalUserManagerImpl+OSLoggable.swift */; };
9496
3C5929E52CAE523E0020D6FF /* MockUserJwtInvalidatedListener.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C5929E42CAE523E0020D6FF /* MockUserJwtInvalidatedListener.swift */; };
9597
3C62999F2BEEA34800649187 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 3C62999E2BEEA34800649187 /* PrivacyInfo.xcprivacy */; };
@@ -1279,6 +1281,8 @@
12791281
3C47A973292642B100312125 /* OneSignalConfigManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OneSignalConfigManager.m; sourceTree = "<group>"; };
12801282
3C4F9E4328A4466C009F453A /* OSOperationRepo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSOperationRepo.swift; sourceTree = "<group>"; };
12811283
3C5117162B15C31E00563465 /* OSUserState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSUserState.swift; sourceTree = "<group>"; };
1284+
3C55013E2E09CF0100E77DF7 /* OSCopyOnWriteSet.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OSCopyOnWriteSet.h; sourceTree = "<group>"; };
1285+
3C55013F2E09CF0100E77DF7 /* OSCopyOnWriteSet.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OSCopyOnWriteSet.m; sourceTree = "<group>"; };
12821286
3C5929E22CAD9EC50020D6FF /* OneSignalUserManagerImpl+OSLoggable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "OneSignalUserManagerImpl+OSLoggable.swift"; sourceTree = "<group>"; };
12831287
3C5929E42CAE523E0020D6FF /* MockUserJwtInvalidatedListener.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockUserJwtInvalidatedListener.swift; sourceTree = "<group>"; };
12841288
3C62999E2BEEA34800649187 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
@@ -2695,6 +2699,8 @@
26952699
DEBAAEB42A436D5D00BF2C1C /* OSStubLocation.m */,
26962700
DEBA2A272C24D0ED00E234DB /* OSBundleUtils.h */,
26972701
DEBA2A252C20E9AA00E234DB /* OSBundleUtils.m */,
2702+
3C55013E2E09CF0100E77DF7 /* OSCopyOnWriteSet.h */,
2703+
3C55013F2E09CF0100E77DF7 /* OSCopyOnWriteSet.m */,
26982704
);
26992705
path = Source;
27002706
sourceTree = "<group>";
@@ -3220,6 +3226,7 @@
32203226
DE7D182A270271A9002D3A5D /* OneSignalCommonDefines.h in Headers */,
32213227
3CE8CC522911AE90000DB0D3 /* OSNetworkingUtils.h in Headers */,
32223228
DEBAAEB02A435B4D00BF2C1C /* OSLocation.h in Headers */,
3229+
3C5501402E09CF0100E77DF7 /* OSCopyOnWriteSet.h in Headers */,
32233230
DE971754274C48CF00FC409E /* OSPrivacyConsentController.h in Headers */,
32243231
3CE8CC4E2911ADD1000DB0D3 /* OSDeviceUtils.h in Headers */,
32253232
3C47A974292642B100312125 /* OneSignalConfigManager.h in Headers */,
@@ -4531,6 +4538,7 @@
45314538
DE7D182E270275FA002D3A5D /* OneSignalTrackFirebaseAnalytics.m in Sources */,
45324539
DE51DDE5294262AB0073D5C4 /* OSRemoteParamController.m in Sources */,
45334540
DE7D182827026F86002D3A5D /* OneSignalUserDefaults.m in Sources */,
4541+
3C5501412E09CF0100E77DF7 /* OSCopyOnWriteSet.m in Sources */,
45344542
3CC063942B6D6B6B002BB07F /* OneSignalCore.m in Sources */,
45354543
);
45364544
runOnlyForDeploymentPostprocessing = 0;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2025 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#import <Foundation/Foundation.h>
29+
30+
/**
31+
Usage of this class should be limited to cases where modification is rare but reads are frequent.
32+
*/
33+
@interface OSCopyOnWriteSet<__covariant ObjectType> : NSObject {
34+
NSMutableSet *_set;
35+
NSLock *_lock;
36+
}
37+
38+
- (instancetype)init;
39+
- (void)addObject:(ObjectType)object;
40+
- (void)removeObject:(ObjectType)object;
41+
- (NSSet *)allObjects;
42+
43+
@end
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2025 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#import "OSCopyOnWriteSet.h"
29+
30+
@implementation OSCopyOnWriteSet
31+
32+
- (instancetype)init {
33+
if (self = [super init]) {
34+
_set = [[NSMutableSet alloc] init];
35+
_lock = [[NSLock alloc] init];
36+
}
37+
return self;
38+
}
39+
40+
- (void)addObject:(id)object {
41+
[_lock lock];
42+
43+
// Create a new copy and modify it
44+
NSMutableSet *newSet = [_set mutableCopy];
45+
[newSet addObject:object];
46+
47+
// Update the internal set
48+
_set = newSet;
49+
50+
[_lock unlock];
51+
}
52+
53+
- (void)removeObject:(id)object {
54+
[_lock lock];
55+
56+
// Create a new copy and modify it
57+
NSMutableSet *newSet = [_set mutableCopy];
58+
[newSet removeObject:object];
59+
60+
// Update the internal set
61+
_set = newSet;
62+
63+
[_lock unlock];
64+
}
65+
66+
- (NSSet *)allObjects {
67+
// Read operation - no lock needed
68+
return _set;
69+
}
70+
71+
@end

0 commit comments

Comments
 (0)