Skip to content

Commit 256f957

Browse files
committed
Create class OSCopyOnWriteSet
1 parent dc58e2a commit 256f957

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
@@ -86,6 +86,8 @@
8686
3C47A975292642B100312125 /* OneSignalConfigManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 3C47A973292642B100312125 /* OneSignalConfigManager.m */; };
8787
3C4F9E4428A4466C009F453A /* OSOperationRepo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C4F9E4328A4466C009F453A /* OSOperationRepo.swift */; };
8888
3C5117172B15C31E00563465 /* OSUserState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C5117162B15C31E00563465 /* OSUserState.swift */; };
89+
3C5501402E09CF0100E77DF7 /* OSCopyOnWriteSet.h in Headers */ = {isa = PBXBuildFile; fileRef = 3C55013E2E09CF0100E77DF7 /* OSCopyOnWriteSet.h */; };
90+
3C5501412E09CF0100E77DF7 /* OSCopyOnWriteSet.m in Sources */ = {isa = PBXBuildFile; fileRef = 3C55013F2E09CF0100E77DF7 /* OSCopyOnWriteSet.m */; };
8991
3C62999F2BEEA34800649187 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 3C62999E2BEEA34800649187 /* PrivacyInfo.xcprivacy */; };
9092
3C6299A12BEEA38100649187 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 3C6299A02BEEA38100649187 /* PrivacyInfo.xcprivacy */; };
9193
3C6299A32BEEA3CC00649187 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 3C6299A22BEEA3CC00649187 /* PrivacyInfo.xcprivacy */; };
@@ -1262,6 +1264,8 @@
12621264
3C47A973292642B100312125 /* OneSignalConfigManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OneSignalConfigManager.m; sourceTree = "<group>"; };
12631265
3C4F9E4328A4466C009F453A /* OSOperationRepo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSOperationRepo.swift; sourceTree = "<group>"; };
12641266
3C5117162B15C31E00563465 /* OSUserState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSUserState.swift; sourceTree = "<group>"; };
1267+
3C55013E2E09CF0100E77DF7 /* OSCopyOnWriteSet.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OSCopyOnWriteSet.h; sourceTree = "<group>"; };
1268+
3C55013F2E09CF0100E77DF7 /* OSCopyOnWriteSet.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OSCopyOnWriteSet.m; sourceTree = "<group>"; };
12651269
3C62999E2BEEA34800649187 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
12661270
3C6299A02BEEA38100649187 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
12671271
3C6299A22BEEA3CC00649187 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
@@ -2632,6 +2636,8 @@
26322636
DEBAAEB42A436D5D00BF2C1C /* OSStubLocation.m */,
26332637
DEBA2A272C24D0ED00E234DB /* OSBundleUtils.h */,
26342638
DEBA2A252C20E9AA00E234DB /* OSBundleUtils.m */,
2639+
3C55013E2E09CF0100E77DF7 /* OSCopyOnWriteSet.h */,
2640+
3C55013F2E09CF0100E77DF7 /* OSCopyOnWriteSet.m */,
26352641
);
26362642
path = Source;
26372643
sourceTree = "<group>";
@@ -3157,6 +3163,7 @@
31573163
DE7D182A270271A9002D3A5D /* OneSignalCommonDefines.h in Headers */,
31583164
3CE8CC522911AE90000DB0D3 /* OSNetworkingUtils.h in Headers */,
31593165
DEBAAEB02A435B4D00BF2C1C /* OSLocation.h in Headers */,
3166+
3C5501402E09CF0100E77DF7 /* OSCopyOnWriteSet.h in Headers */,
31603167
DE971754274C48CF00FC409E /* OSPrivacyConsentController.h in Headers */,
31613168
3CE8CC4E2911ADD1000DB0D3 /* OSDeviceUtils.h in Headers */,
31623169
3C47A974292642B100312125 /* OneSignalConfigManager.h in Headers */,
@@ -4455,6 +4462,7 @@
44554462
DE7D182E270275FA002D3A5D /* OneSignalTrackFirebaseAnalytics.m in Sources */,
44564463
DE51DDE5294262AB0073D5C4 /* OSRemoteParamController.m in Sources */,
44574464
DE7D182827026F86002D3A5D /* OneSignalUserDefaults.m in Sources */,
4465+
3C5501412E09CF0100E77DF7 /* OSCopyOnWriteSet.m in Sources */,
44584466
3CC063942B6D6B6B002BB07F /* OneSignalCore.m in Sources */,
44594467
);
44604468
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)