Skip to content

Commit ffebc25

Browse files
committed
add hydrating check when a model is added to store
* Motivation: hydrating subscriptions can create a model and add to store if it doesn't exist locally. Guard against hydrating here as well so that a request isn't extraneously created.
1 parent 01e1ad9 commit ffebc25

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

iOS_SDK/OneSignalSDK/OneSignalOSCore/Source/OSModelStore.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ open class OSModelStore<TModel: OSModel>: NSObject {
7171
return self.models
7272
}
7373

74-
public func add(id: String, model: TModel) {
74+
public func add(id: String, model: TModel, hydrating: Bool) {
7575
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OSModelStore add() called with model \(model)")
7676
// TODO: Check if we are adding the same model? Do we replace?
7777
// For example, calling addEmail multiple times with the same email
@@ -82,7 +82,11 @@ open class OSModelStore<TModel: OSModel>: NSObject {
8282

8383
// listen for changes to this model
8484
model.changeNotifier.subscribe(self)
85-
85+
86+
guard !hydrating else {
87+
return
88+
}
89+
8690
self.changeSubscription.fire { modelStoreListener in
8791
modelStoreListener.onAdded(model)
8892
}

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OSUserRequests.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,13 @@ class OSUserExecutor {
113113

114114
} else {
115115
// This subscription does not exist in the store, add
116-
// TODO: This creates a Delta and sends a request when it should be hydrating
117116
OneSignalUserManagerImpl.sharedInstance.subscriptionModelStore.add(id: address, model: OSSubscriptionModel(
118117
type: type,
119118
address: subModel["token"] as? String,
120119
subscriptionId: subModel["id"] as? String,
121120
accepted: true,
122121
isDisabled: false,
123-
changeNotifier: OSEventProducer())
122+
changeNotifier: OSEventProducer()), hydrating: true
124123
)
125124
}
126125
}

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OneSignalUserManagerImpl.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,18 +334,18 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
334334
}
335335

336336
let identityModel = OSIdentityModel(aliases: aliases, changeNotifier: OSEventProducer())
337-
self.identityModelStore.add(id: OS_IDENTITY_MODEL_KEY, model: identityModel)
337+
self.identityModelStore.add(id: OS_IDENTITY_MODEL_KEY, model: identityModel, hydrating: false)
338338

339339
let propertiesModel = OSPropertiesModel(changeNotifier: OSEventProducer())
340-
self.propertiesModelStore.add(id: OS_PROPERTIES_MODEL_KEY, model: propertiesModel)
340+
self.propertiesModelStore.add(id: OS_PROPERTIES_MODEL_KEY, model: propertiesModel, hydrating: false)
341341

342342
// TODO: We will have to save subscription_id and push_token to user defaults when we get them
343343

344344
let pushSubscription = pushSubscriptionModel ?? createDefaultPushSubscription()
345345

346346
// Add pushSubscription to store if not present
347347
if (!pushSubscriptionModelStore.getModels().keys.contains(OS_PUSH_SUBSCRIPTION_MODEL_KEY)) {
348-
pushSubscriptionModelStore.add(id: OS_PUSH_SUBSCRIPTION_MODEL_KEY, model: pushSubscription)
348+
pushSubscriptionModelStore.add(id: OS_PUSH_SUBSCRIPTION_MODEL_KEY, model: pushSubscription, hydrating: false)
349349
}
350350

351351
_user = OSUserInternalImpl(identityModel: identityModel, propertiesModel: propertiesModel, pushSubscriptionModel: pushSubscription)
@@ -558,7 +558,7 @@ extension OneSignalUserManagerImpl: OSUser {
558558
isDisabled: false,
559559
changeNotifier: OSEventProducer()
560560
)
561-
self.subscriptionModelStore.add(id: email, model: model)
561+
self.subscriptionModelStore.add(id: email, model: model, hydrating: false)
562562
}
563563

564564
/**
@@ -590,7 +590,7 @@ extension OneSignalUserManagerImpl: OSUser {
590590
isDisabled: false,
591591
changeNotifier: OSEventProducer()
592592
)
593-
self.subscriptionModelStore.add(id: number, model: model)
593+
self.subscriptionModelStore.add(id: number, model: model, hydrating: false)
594594
}
595595

596596
/**

0 commit comments

Comments
 (0)