Skip to content

Commit f9cf364

Browse files
committed
Have an Identity Model Repo for models still being used
* Introduce OSIdentityModelRepo managed by the User Manager where a dictionary of identity models live * This will hold identity models added when users change * When requests are uncached, their identity models will also be added as they are still being used. * This is a lightweight option over modifying the Identity Model Store (which is an option if we wish to use the model store to manage all identity models)
1 parent 88e31f5 commit f9cf364

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
3C14E3A12AFAE461006ED053 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 3C14E3A02AFAE461006ED053 /* PrivacyInfo.xcprivacy */; };
6969
3C14E3A42AFAE54C006ED053 /* OneSignalSwiftInterface.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEC08AFF2947D4E900C81DA3 /* OneSignalSwiftInterface.swift */; };
7070
3C24B0EC2BD09D7A0052E771 /* OneSignalCoreObjCTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 3C24B0EB2BD09D7A0052E771 /* OneSignalCoreObjCTests.m */; };
71+
3C277D7E2BD76E0000857606 /* OSIdentityModelRepo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C277D7D2BD76E0000857606 /* OSIdentityModelRepo.swift */; };
7172
3C2C7DC8288F3C020020F9AE /* OSSubscriptionModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C2C7DC7288F3C020020F9AE /* OSSubscriptionModel.swift */; };
7273
3C2D8A5928B4C4E300BE41F6 /* OSDelta.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C2D8A5828B4C4E300BE41F6 /* OSDelta.swift */; };
7374
3C44673E296D099D0039A49E /* OneSignalMobileProvision.m in Sources */ = {isa = PBXBuildFile; fileRef = 912411FD1E73342200E41FD7 /* OneSignalMobileProvision.m */; };
@@ -963,6 +964,7 @@
963964
3C14E3A02AFAE461006ED053 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
964965
3C24B0EA2BD09D790052E771 /* OneSignalCoreTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "OneSignalCoreTests-Bridging-Header.h"; sourceTree = "<group>"; };
965966
3C24B0EB2BD09D7A0052E771 /* OneSignalCoreObjCTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OneSignalCoreObjCTests.m; sourceTree = "<group>"; };
967+
3C277D7D2BD76E0000857606 /* OSIdentityModelRepo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSIdentityModelRepo.swift; sourceTree = "<group>"; };
966968
3C2C7DC2288E007E0020F9AE /* UnitTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UnitTests-Bridging-Header.h"; sourceTree = "<group>"; };
967969
3C2C7DC7288F3C020020F9AE /* OSSubscriptionModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSSubscriptionModel.swift; sourceTree = "<group>"; };
968970
3C2D8A5828B4C4E300BE41F6 /* OSDelta.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSDelta.swift; sourceTree = "<group>"; };
@@ -1982,6 +1984,7 @@
19821984
DE69E1A9282ED8790090BB3D /* UnitTestApp-Bridging-Header.h */,
19831985
3C0EF49D28A1DBCB00E5434B /* OSUserInternalImpl.swift */,
19841986
DE69E1AA282ED8790090BB3D /* OneSignalUserManagerImpl.swift */,
1987+
3C277D7D2BD76E0000857606 /* OSIdentityModelRepo.swift */,
19851988
3C2C7DC7288F3C020020F9AE /* OSSubscriptionModel.swift */,
19861989
3CE92279289FA88B001B1062 /* OSIdentityModelStoreListener.swift */,
19871990
3CF8629D28A183F900776CA4 /* OSIdentityModel.swift */,
@@ -3519,6 +3522,7 @@
35193522
3CF862A028A1964F00776CA4 /* OSPropertiesModel.swift in Sources */,
35203523
3C8E6E0128AC0BA10031E48A /* OSIdentityOperationExecutor.swift in Sources */,
35213524
3CF862A228A197D200776CA4 /* OSPropertiesModelStoreListener.swift in Sources */,
3525+
3C277D7E2BD76E0000857606 /* OSIdentityModelRepo.swift in Sources */,
35223526
3C9AD6C12B22886600BC1540 /* OSRequestUpdateSubscription.swift in Sources */,
35233527
3C0EF49E28A1DBCB00E5434B /* OSUserInternalImpl.swift in Sources */,
35243528
3C8E6DFF28AB09AE0031E48A /* OSPropertyOperationExecutor.swift in Sources */,
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Modified MIT License
3+
4+
Copyright 2024 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
29+
30+
/**
31+
This class stores all Identity Models that are being used during an app session.
32+
Its purpose is to manage the instances for all referencing objects.
33+
The models are built up on each new cold start, so no caching occurs.
34+
35+
When are Identity Models added to this repo?
36+
1. When the User Manager starts, and the Identity Model is loaded from cache.
37+
2. When users switch and new Identity Models are created.
38+
3. Identity Models are added when requests are uncached.
39+
*/
40+
class OSIdentityModelRepo {
41+
let lock = NSLock()
42+
var models: [String: OSIdentityModel] = [:]
43+
44+
func add(model: OSIdentityModel) {
45+
lock.withLock {
46+
models[model.modelId] = model
47+
}
48+
}
49+
50+
func get(modelId: String) -> OSIdentityModel? {
51+
lock.withLock {
52+
return models[modelId]
53+
}
54+
}
55+
}

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OneSignalUserManagerImpl.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
118118

119119
@objc public let pushSubscriptionImpl: OSPushSubscriptionImpl
120120

121+
var identityModelRepo = OSIdentityModelRepo()
122+
121123
private var hasCalledStart = false
122124

123125
private var jwtExpiredHandler: OSJwtExpiredHandler?
@@ -211,12 +213,14 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
211213
let pushSubscription = pushSubscriptionModelStore.getModels()[OS_PUSH_SUBSCRIPTION_MODEL_KEY] {
212214
hasCachedUser = true
213215
_user = OSUserInternalImpl(identityModel: identityModel, propertiesModel: propertiesModel, pushSubscriptionModel: pushSubscription)
216+
addIdentityModelToRepo(identityModel)
214217
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignalUserManager.start called, loaded the user from cache.")
215218
}
216219

217220
// TODO: Update the push sub model with any new state from NotificationsManager
218221

219222
// Setup the executors
223+
// The OSUserExecutor has to run first, before other executors
220224
OSUserExecutor.start()
221225
OSOperationRepo.sharedInstance.start()
222226

@@ -253,6 +257,14 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
253257
}
254258
}
255259

260+
func addIdentityModelToRepo(_ model: OSIdentityModel) {
261+
self.identityModelRepo.add(model: model)
262+
}
263+
264+
func getIdentityModel(_ modelId: String) -> OSIdentityModel? {
265+
return identityModelRepo.get(modelId: modelId)
266+
}
267+
256268
@objc
257269
public func login(externalId: String, token: String?) {
258270
guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: nil) else {
@@ -429,6 +441,7 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
429441

430442
let identityModel = OSIdentityModel(aliases: aliases, changeNotifier: OSEventProducer())
431443
self.identityModelStore.add(id: OS_IDENTITY_MODEL_KEY, model: identityModel, hydrating: false)
444+
self.addIdentityModelToRepo(identityModel)
432445

433446
let propertiesModel = OSPropertiesModel(changeNotifier: OSEventProducer())
434447
self.propertiesModelStore.add(id: OS_PROPERTIES_MODEL_KEY, model: propertiesModel, hydrating: false)

iOS_SDK/OneSignalSDK/OneSignalUserMocks/OneSignalUserMocks.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public class OneSignalUserMocks: NSObject {
4242
public static func resetStaticUserExecutor() {
4343
OSUserExecutor.userRequestQueue.removeAll()
4444
OSUserExecutor.transferSubscriptionRequestQueue.removeAll()
45-
OSUserExecutor.identityModels.removeAll()
4645
}
4746

4847
/**
@@ -51,6 +50,8 @@ public class OneSignalUserMocks: NSObject {
5150
This is adapting as more data needs to be considered and reset...
5251
*/
5352
public static func resetUserManager() {
53+
OneSignalUserManagerImpl.sharedInstance.identityModelRepo.reset()
54+
5455
OneSignalUserManagerImpl.sharedInstance.identityModelStore.clearModelsFromStore()
5556
OneSignalUserManagerImpl.sharedInstance.propertiesModelStore.clearModelsFromStore()
5657
OneSignalUserManagerImpl.sharedInstance.subscriptionModelStore.clearModelsFromStore()
@@ -72,3 +73,9 @@ public class OneSignalUserMocks: NSObject {
7273
OSOperationRepo.sharedInstance.addExecutor(subscriptionExecutor)
7374
}
7475
}
76+
77+
extension OSIdentityModelRepo {
78+
func reset() {
79+
self.models = [:]
80+
}
81+
}

0 commit comments

Comments
 (0)