Skip to content

Commit e0f80e9

Browse files
committed
can't initialize executors until later
* Motivation: Must be careful of accessing `OneSignalUserManagerImpl.sharedInstance` before it's actually finished initializing, as the initializer for it does a lot of things! * What was happening was that during the process of initializing the `sharedInstance`, we initialized the executors, but the executors' initializer accessed model stores via OneSignalUserManagerImpl.sharedInstance` which is a problem. * Instead, initialize these in `start()`, but now they will be Optional. * And we may need to note if any other areas can have the same issue.
1 parent 059227c commit e0f80e9

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OneSignalUserManagerImpl.swift

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
154154
let subscriptionModelStoreListener: OSSubscriptionModelStoreListener
155155
let pushSubscriptionModelStoreListener: OSSubscriptionModelStoreListener
156156

157-
// has Property and Identity operation executors
158-
let propertyExecutor = OSPropertyOperationExecutor()
159-
let identityExecutor = OSIdentityOperationExecutor()
160-
let subscriptionExecutor = OSSubscriptionOperationExecutor()
157+
// Executors must be initialize after sharedInstance is initialized
158+
var propertyExecutor: OSPropertyOperationExecutor?
159+
var identityExecutor: OSIdentityOperationExecutor?
160+
var subscriptionExecutor: OSSubscriptionOperationExecutor?
161161

162162
private override init() {
163163
self.identityModelStoreListener = OSIdentityModelStoreListener(store: identityModelStore)
@@ -201,6 +201,14 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
201201
// Setup the executors
202202
OSUserExecutor.start()
203203
OSOperationRepo.sharedInstance.start()
204+
205+
// Cannot initialize these executors in `init` as they reference the sharedInstance
206+
let propertyExecutor = OSPropertyOperationExecutor()
207+
let identityExecutor = OSIdentityOperationExecutor()
208+
let subscriptionExecutor = OSSubscriptionOperationExecutor()
209+
self.propertyExecutor = propertyExecutor
210+
self.identityExecutor = identityExecutor
211+
self.subscriptionExecutor = subscriptionExecutor
204212
OSOperationRepo.sharedInstance.addExecutor(identityExecutor)
205213
OSOperationRepo.sharedInstance.addExecutor(propertyExecutor)
206214
OSOperationRepo.sharedInstance.addExecutor(subscriptionExecutor)
@@ -399,12 +407,18 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
399407
let identityModel = user.identityModel
400408
let propertiesModel = user.propertiesModel
401409
let propertiesDeltas = OSPropertiesDeltas(sessionTime: nil, sessionCount: nil, amountSpent: nil, purchases: purchases)
402-
propertyExecutor.updateProperties(
403-
propertiesDeltas: propertiesDeltas,
404-
refreshDeviceMetadata: false,
405-
propertiesModel: propertiesModel,
406-
identityModel: identityModel
407-
)
410+
411+
// propertyExecutor should exist as this should be called after `start()` has been called
412+
if let propertyExecutor = self.propertyExecutor {
413+
propertyExecutor.updateProperties(
414+
propertiesDeltas: propertiesDeltas,
415+
refreshDeviceMetadata: false,
416+
propertiesModel: propertiesModel,
417+
identityModel: identityModel
418+
)
419+
} else {
420+
OneSignalLog.onesignalLog(.LL_ERROR, message: "OneSignalUserManagerImpl.sendPurchases with purchases: \(purchases) cannot be executed due to missing property executor.")
421+
}
408422
}
409423

410424

@@ -449,12 +463,18 @@ extension OneSignalUserManagerImpl {
449463
let identityModel = user.identityModel
450464
let propertiesModel = user.propertiesModel
451465
let propertiesDeltas = OSPropertiesDeltas(sessionTime: sessionTime, sessionCount: sessionCount, amountSpent: nil, purchases: nil)
452-
propertyExecutor.updateProperties(
453-
propertiesDeltas: propertiesDeltas,
454-
refreshDeviceMetadata: refreshDeviceMetadata,
455-
propertiesModel: propertiesModel,
456-
identityModel: identityModel
457-
)
466+
467+
// propertyExecutor should exist as this should be called after `start()` has been called
468+
if let propertyExecutor = self.propertyExecutor {
469+
propertyExecutor.updateProperties(
470+
propertiesDeltas: propertiesDeltas,
471+
refreshDeviceMetadata: refreshDeviceMetadata,
472+
propertiesModel: propertiesModel,
473+
identityModel: identityModel
474+
)
475+
} else {
476+
OneSignalLog.onesignalLog(.LL_ERROR, message: "OneSignalUserManagerImpl.updateSession with sessionCount: \(String(describing: sessionCount)) sessionTime: \(String(describing: sessionTime)) cannot be executed due to missing property executor.")
477+
}
458478
}
459479

460480
/**

0 commit comments

Comments
 (0)