diff --git a/iOS_SDK/OneSignalSDK/OneSignalOSCore/Source/OSOperationRepo.swift b/iOS_SDK/OneSignalSDK/OneSignalOSCore/Source/OSOperationRepo.swift index 338fadd50..2e280825f 100644 --- a/iOS_SDK/OneSignalSDK/OneSignalOSCore/Source/OSOperationRepo.swift +++ b/iOS_SDK/OneSignalSDK/OneSignalOSCore/Source/OSOperationRepo.swift @@ -107,7 +107,7 @@ public class OSOperationRepo: NSObject { // TODO: We can make this method internal once there is no manual adding of a Delta except through stores. This can happen when session data and purchase data use the model / store / listener infrastructure. */ - public func enqueueDelta(_ delta: OSDelta) { + public func enqueueDelta(_ delta: OSDelta, flush: Bool = false) { guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: nil) else { return } @@ -115,8 +115,13 @@ public class OSOperationRepo: NSObject { self.dispatchQueue.async { OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OSOperationRepo enqueueDelta: \(delta)") self.deltaQueue.append(delta) + // Persist the deltas (including new delta) to storage OneSignalUserDefaults.initShared().saveCodeableData(forKey: OS_OPERATION_REPO_DELTA_QUEUE_KEY, withValue: self.deltaQueue) + + if flush { + self.flushDeltaQueue() + } } } diff --git a/iOS_SDK/OneSignalSDK/OneSignalUser/Source/OneSignalUserManagerImpl.swift b/iOS_SDK/OneSignalSDK/OneSignalUser/Source/OneSignalUserManagerImpl.swift index 77ecd4c79..710d3724b 100644 --- a/iOS_SDK/OneSignalSDK/OneSignalUser/Source/OneSignalUserManagerImpl.swift +++ b/iOS_SDK/OneSignalSDK/OneSignalUser/Source/OneSignalUserManagerImpl.swift @@ -551,7 +551,7 @@ extension OneSignalUserManagerImpl { userExecutor!.executePendingRequests() OSOperationRepo.sharedInstance.paused = false - updatePropertiesDeltas(property: .session_count, value: 1) + updatePropertiesDeltas(property: .session_count, value: 1, flush: true) // Fetch the user's data if there is a onesignal_id if let onesignalId = onesignalId { @@ -567,7 +567,7 @@ extension OneSignalUserManagerImpl { /// It enqueues an OSDelta to the Operation Repo. /// /// - Parameter property:Expected inputs are `.session_time"`, `.session_count"`, and `.purchases"`. - func updatePropertiesDeltas(property: OSPropertiesSupportedProperty, value: Any) { + func updatePropertiesDeltas(property: OSPropertiesSupportedProperty, value: Any, flush: Bool = false) { guard !OneSignalConfigManager.shouldAwaitAppIdAndLogMissingPrivacyConsent(forMethod: "updatePropertiesDeltas") else { return } @@ -583,7 +583,7 @@ extension OneSignalUserManagerImpl { property: property.rawValue, value: value ) - OSOperationRepo.sharedInstance.enqueueDelta(delta) + OSOperationRepo.sharedInstance.enqueueDelta(delta, flush: flush) } /// Time processors forward the session time to this method. diff --git a/iOS_SDK/OneSignalSDK/OneSignalUserTests/OneSignalUserTests.swift b/iOS_SDK/OneSignalSDK/OneSignalUserTests/OneSignalUserTests.swift index 3a4bf1745..5e5673676 100644 --- a/iOS_SDK/OneSignalSDK/OneSignalUserTests/OneSignalUserTests.swift +++ b/iOS_SDK/OneSignalSDK/OneSignalUserTests/OneSignalUserTests.swift @@ -87,10 +87,8 @@ final class OneSignalUserTests: XCTestCase { /* When */ OneSignalUserManagerImpl.sharedInstance.sendSessionTime(100) - - // This adds a `session_count` property with value of 1 - // It also sets `refresh_device_metadata` to `true` - OneSignalUserManagerImpl.sharedInstance.startNewSession() + + OneSignalUserManagerImpl.sharedInstance.updatePropertiesDeltas(property: .session_count, value: 1, flush: false) OneSignalUserManagerImpl.sharedInstance.setLanguage("lang_1") @@ -108,7 +106,6 @@ final class OneSignalUserTests: XCTestCase { OneSignalUserManagerImpl.sharedInstance.addTags(["a": "a", "b": "b", "c": "c"]) - OneSignalUserManagerImpl.sharedInstance.startNewSession() let purchases = [ ["sku": "sku1", "amount": "1.25", "iso": "USD"], @@ -118,6 +115,10 @@ final class OneSignalUserTests: XCTestCase { OneSignalUserManagerImpl.sharedInstance.sendPurchases(purchases as [[String: AnyObject]]) OneSignalUserManagerImpl.sharedInstance.setLocation(latitude: 111.111, longitude: 222.222) + + // This adds a `session_count` property with value of 1 + // It also sets `refresh_device_metadata` to `true` + OneSignalUserManagerImpl.sharedInstance.startNewSession() /* Then */