Skip to content

Commit 9bc16f4

Browse files
committed
refresh model stores + backfill mirror in start()
Refresh all four model stores at the top of start() so a singleton that was first touched during iOS prewarm (UD locked, dicts loaded empty) sees what's actually on disk before Path 1's cache check. Backfill OSResilientStorage.keySubscriptionId from Path 1 and the v3 legacy migration so SDK upgraders and migrated users populate the mirror on their first post-upgrade launch. Write OSResilientStorage.keyHasPriorSession at the end of start(); drain via snapshot() so an OS kill can't lose the sentinel.
1 parent 33bbd80 commit 9bc16f4

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OneSignalUserManagerImpl.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,16 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
215215

216216
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignalUserManager calling start")
217217

218+
// The model stores load their in-memory `models` dict once in their initializer.
219+
// If the singleton was first touched while protected data was unavailable (iOS app
220+
// prewarm), that read returned nil and the dicts are empty. The gate above ensures
221+
// `start()` only proceeds when protected data is available, but the stores need to
222+
// be refreshed here so Path 1's cache check can see what's on disk.
223+
identityModelStore.refresh()
224+
propertiesModelStore.refresh()
225+
subscriptionModelStore.refresh()
226+
pushSubscriptionModelStore.refresh()
227+
218228
OSNotificationsManager.delegate = self
219229

220230
var hasCachedUser = false
@@ -228,6 +238,11 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
228238
_user = OSUserInternalImpl(identityModel: identityModel, propertiesModel: propertiesModel, pushSubscriptionModel: pushSubscription)
229239
addIdentityModelToRepo(identityModel)
230240
OneSignalLog.onesignalLog(.LL_VERBOSE, message: "OneSignalUserManager.start called, loaded the user from cache.")
241+
242+
// Backfill the mirror so SDK upgraders to v5.5.2 populate it on first normal launch.
243+
if let subId = pushSubscription.subscriptionId, !subId.isEmpty {
244+
OSResilientStorage.setString(subId, forKey: OSResilientStorage.keySubscriptionId)
245+
}
231246
}
232247

233248
// TODO: Update the push sub model with any new state from NotificationsManager
@@ -256,6 +271,7 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
256271
OneSignalLog.onesignalLog(.LL_DEBUG, message: "OneSignalUserManager: creating user linked to legacy subscription \(legacyPlayerId)")
257272
createUserFromLegacyPlayer(legacyPlayerId)
258273
OneSignalUserDefaults.initShared().saveString(forKey: OSUD_PUSH_SUBSCRIPTION_ID, withValue: legacyPlayerId)
274+
OSResilientStorage.setString(legacyPlayerId, forKey: OSResilientStorage.keySubscriptionId)
259275
OneSignalUserDefaults.initStandard().removeValue(forKey: OSUD_LEGACY_PLAYER_ID)
260276
OneSignalUserDefaults.initShared().removeValue(forKey: OSUD_LEGACY_PLAYER_ID)
261277
} else {
@@ -275,6 +291,13 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
275291
_user?.update()
276292
}
277293
hasCalledStart = true
294+
// Mark this app install as having completed `start()` at least once — see
295+
// `OSResilientStorage.keyHasPriorSession`. The `snapshot()` below forces the
296+
// async write queue to drain before returning, so an OS kill in the
297+
// microsecond window can't lose the sentinel and cause the next prewarm
298+
// launch to misclassify us as a fresh install.
299+
OSResilientStorage.setString("1", forKey: OSResilientStorage.keyHasPriorSession)
300+
_ = OSResilientStorage.snapshot()
278301
}
279302
}
280303

0 commit comments

Comments
 (0)