Skip to content

Commit 604d1e9

Browse files
committed
loading the user from cache considers reading from storage TODO
1 parent 7e3adf1 commit 604d1e9

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OneSignalUserManagerImpl.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,24 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
266266
OneSignalUserDefaults.initShared().saveString(forKey: OSUD_PUSH_SUBSCRIPTION_ID, withValue: legacyPlayerId)
267267
OneSignalUserDefaults.initStandard().removeValue(forKey: OSUD_LEGACY_PLAYER_ID)
268268
OneSignalUserDefaults.initShared().removeValue(forKey: OSUD_LEGACY_PLAYER_ID)
269+
} else if !hasCachedUser,
270+
_user == nil,
271+
let cachedSubscriptionId = OSResilientStorage.string(forKey: OSResilientStorage.keySubscriptionId),
272+
!cachedSubscriptionId.isEmpty
273+
{
274+
// TODO: In this state, we don't want to persist anything to UserDefaults and overwrite correct data with half data.
275+
// TODO: However, in this state where we can't read UserDefaults, does writing to it fail automatically?
276+
// Path 2.5. Model stores loaded empty but the resilient cache shows we've
277+
// initialized before: the prewarm-before-first-unlock case. Recover identity
278+
// locally; missing properties/aliases will rehydrate on next fetchUser.
279+
let cachedOnesignalId = OSResilientStorage.string(forKey: OSResilientStorage.keyOneSignalId)
280+
OneSignalLog.onesignalLog(.LL_DEBUG, message: "OneSignalUserManager: recovering user from cache subscription_id=\(cachedSubscriptionId) onesignal_id=\(cachedOnesignalId ?? "(nil)")")
281+
282+
let pushSubscriptionModel = createDefaultPushSubscription(subscriptionId: cachedSubscriptionId)
283+
let recoveredUser = setNewInternalUser(externalId: nil, pushSubscriptionModel: pushSubscriptionModel)
284+
if let onesignalId = cachedOnesignalId, !onesignalId.isEmpty {
285+
recoveredUser.identityModel.hydrate([OS_ONESIGNAL_ID: onesignalId])
286+
}
269287
} else {
270288
// Path 3. Creates an anonymous user if there isn't one in the cache nor a legacy player
271289
if _user == nil {

0 commit comments

Comments
 (0)