Skip to content

Commit 01e1ad9

Browse files
committed
have a separate model store just for push sub
* Motivation: push sub behaves differently from other subscriptions and is not cleared from the store when a user changes. Separate it into its own store so that the email and sms subscriptions stores can be cleared but the push subscription store is not.
1 parent e396cee commit 01e1ad9

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

iOS_SDK/OneSignalSDK/OneSignalCore/Source/OneSignalCommonDefines.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ typedef enum {GET, POST, HEAD, PUT, DELETE, OPTIONS, CONNECT, TRACE, PATCH} HTTP
291291
#define OS_PROPERTIES_MODEL_KEY @"OS_PROPERTIES_MODEL_KEY"
292292
#define OS_PROPERTIES_MODEL_STORE_KEY @"OS_PROPERTIES_MODEL_STORE_KEY"
293293
#define OS_PUSH_SUBSCRIPTION_MODEL_KEY @"OS_PUSH_SUBSCRIPTION_MODEL_KEY"
294+
#define OS_PUSH_SUBSCRIPTION_MODEL_STORE_KEY @"OS_PUSH_SUBSCRIPTION_MODEL_STORE_KEY"
294295
#define OS_SUBSCRIPTION_MODEL_STORE_KEY @"OS_SUBSCRIPTION_MODEL_STORE_KEY"
295296

296297
#define OS_ADD_ALIAS_DELTA @"OS_ADD_ALIAS_DELTA"

iOS_SDK/OneSignalSDK/OneSignalOSCore/Source/OSModelStore.swift

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,8 @@ open class OSModelStore<TModel: OSModel>: NSObject {
126126
When the User changes, the Subscription Model Store must remove all models.
127127
In contrast, it is not necessary for the Identity or Properties Model Stores to do so.
128128
*/
129-
public func clearModelsFromStore(modelToKeepId: String?) {
130-
// TODO: For now, uglily have param for model to keep as workaround for clearing all models except push sub
131-
// When we have a new user, the subscription store should be cleared except for push sub
132-
// That's currently the only model store we call this from.
133-
if let modelToKeepId,
134-
let modelToKeep = self.models[modelToKeepId]
135-
{
136-
self.models = [modelToKeepId: modelToKeep]
137-
OneSignalUserDefaults.initShared().saveCodeableData(forKey: self.storeKey, withValue: self.models)
138-
} else {
139-
self.models = [:]
140-
}
129+
public func clearModelsFromStore() {
130+
self.models = [:]
141131
}
142132
}
143133

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OneSignalUserManagerImpl.swift

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,19 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
140140
return pushSubscriptionStateChangesObserver
141141
}
142142

143-
// has Identity, Properties, and Subscription Model Stores
143+
// has Identity, Properties, Subscription, and Push Subscription Model Stores
144144
let identityModelStore = OSModelStore<OSIdentityModel>(changeSubscription: OSEventProducer(), storeKey: OS_IDENTITY_MODEL_STORE_KEY)
145145
let propertiesModelStore = OSModelStore<OSPropertiesModel>(changeSubscription: OSEventProducer(), storeKey: OS_PROPERTIES_MODEL_STORE_KEY)
146+
// Holds email and sms subscription models
146147
let subscriptionModelStore = OSModelStore<OSSubscriptionModel>(changeSubscription: OSEventProducer(), storeKey: OS_SUBSCRIPTION_MODEL_STORE_KEY)
148+
// Holds a single push subscription model
149+
let pushSubscriptionModelStore = OSModelStore<OSSubscriptionModel>(changeSubscription: OSEventProducer(), storeKey: OS_PUSH_SUBSCRIPTION_MODEL_STORE_KEY)
147150

148151
// These must be initialized in init()
149152
let identityModelStoreListener: OSIdentityModelStoreListener
150153
let propertiesModelStoreListener: OSPropertiesModelStoreListener
151154
let subscriptionModelStoreListener: OSSubscriptionModelStoreListener
155+
let pushSubscriptionModelStoreListener: OSSubscriptionModelStoreListener
152156

153157
// has Property and Identity operation executors
154158
let propertyExecutor = OSPropertyOperationExecutor()
@@ -159,6 +163,7 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
159163
self.identityModelStoreListener = OSIdentityModelStoreListener(store: identityModelStore)
160164
self.propertiesModelStoreListener = OSPropertiesModelStoreListener(store: propertiesModelStore)
161165
self.subscriptionModelStoreListener = OSSubscriptionModelStoreListener(store: subscriptionModelStore)
166+
self.pushSubscriptionModelStoreListener = OSSubscriptionModelStoreListener(store: pushSubscriptionModelStore)
162167
}
163168

164169
// TODO: This method is called A LOT, check if all calls are needed.
@@ -180,7 +185,7 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
180185
// Corrupted state if any of these models exist without the others
181186
if let identityModel = identityModelStore.getModels()[OS_IDENTITY_MODEL_KEY],
182187
let propertiesModel = propertiesModelStore.getModels()[OS_PROPERTIES_MODEL_KEY],
183-
let pushSubscription = subscriptionModelStore.getModels()[OS_PUSH_SUBSCRIPTION_MODEL_KEY] {
188+
let pushSubscription = pushSubscriptionModelStore.getModels()[OS_PUSH_SUBSCRIPTION_MODEL_KEY] {
184189
_user = OSUserInternalImpl(identityModel: identityModel, propertiesModel: propertiesModel, pushSubscriptionModel: pushSubscription)
185190
}
186191

@@ -191,6 +196,7 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
191196
identityModelStoreListener.start()
192197
propertiesModelStoreListener.start()
193198
subscriptionModelStoreListener.start()
199+
pushSubscriptionModelStoreListener.start()
194200

195201
// Setup the executors
196202
OSUserExecutor.start()
@@ -226,7 +232,7 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
226232
}
227233
}
228234

229-
let pushSubscriptionModel = subscriptionModelStore.getModel(key: OS_PUSH_SUBSCRIPTION_MODEL_KEY)
235+
let pushSubscriptionModel = pushSubscriptionModelStore.getModel(key: OS_PUSH_SUBSCRIPTION_MODEL_KEY)
230236
prepareForNewUser()
231237

232238
let newUser = setNewInternalUser(externalId: externalId, pushSubscriptionModel: pushSubscriptionModel)
@@ -251,7 +257,7 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
251257
let identityModelToIdentify = currentUser.identityModel
252258

253259
// Immediately drop the old user and set a new user in the SDK
254-
let pushSubscriptionModel = subscriptionModelStore.getModel(key: OS_PUSH_SUBSCRIPTION_MODEL_KEY)
260+
let pushSubscriptionModel = pushSubscriptionModelStore.getModel(key: OS_PUSH_SUBSCRIPTION_MODEL_KEY)
255261
prepareForNewUser()
256262
let newUser = setNewInternalUser(externalId: externalId, pushSubscriptionModel: pushSubscriptionModel)
257263

@@ -313,7 +319,7 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
313319
NotificationCenter.default.post(name: Notification.Name(OS_ON_USER_WILL_CHANGE), object: nil)
314320

315321
// This store MUST be cleared, Identity and Properties do not.
316-
subscriptionModelStore.clearModelsFromStore(modelToKeepId: OS_PUSH_SUBSCRIPTION_MODEL_KEY)
322+
subscriptionModelStore.clearModelsFromStore()
317323
}
318324

319325
/**
@@ -338,8 +344,8 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
338344
let pushSubscription = pushSubscriptionModel ?? createDefaultPushSubscription()
339345

340346
// Add pushSubscription to store if not present
341-
if (!subscriptionModelStore.getModels().keys.contains(OS_PUSH_SUBSCRIPTION_MODEL_KEY)) {
342-
subscriptionModelStore.add(id: OS_PUSH_SUBSCRIPTION_MODEL_KEY, model: pushSubscription)
347+
if (!pushSubscriptionModelStore.getModels().keys.contains(OS_PUSH_SUBSCRIPTION_MODEL_KEY)) {
348+
pushSubscriptionModelStore.add(id: OS_PUSH_SUBSCRIPTION_MODEL_KEY, model: pushSubscription)
343349
}
344350

345351
_user = OSUserInternalImpl(identityModel: identityModel, propertiesModel: propertiesModel, pushSubscriptionModel: pushSubscription)

0 commit comments

Comments
 (0)