Add lifecycle callbacks for iOS and tvOS - #1390
Open
olipetrovic wants to merge 1 commit into
Open
Conversation
iOS and tvOS suspend the process while the app is in the background, which freezes the delay-based auto-refresh timer. Without lifecycle callbacks the timer can sleep through the access token's expiry, leaving every request with an expired JWT (PostgREST 401 PGRST303) after the app returns to the foreground - until the user signs out and back in. Mirror the Android implementation via NSNotificationCenter: cancel auto refresh on UIApplicationDidEnterBackgroundNotification, and re-import the session from storage on UIApplicationWillEnterForegroundNotification - refreshing immediately if the token went stale while suspended, or re-arming the timer with the correct remaining time if not. Gated behind the existing enableLifecycleCallbacks config flag. The appleMain setupPlatform actual now delegates to a per-family setupApplePlatform expect: a new uikit source set (iOS + tvOS) holds the observer registration, while macOS (processes are not suspended) and watchOS (lifecycle notifications live in WatchKit, not UIKit) keep the previous initDone-only behavior.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add lifecycle callbacks for iOS and tvOS
iOS and tvOS suspend the process while the app is in the background, which freezes the delay-based auto-refresh timer. Without lifecycle callbacks the timer can sleep through the access token's expiry, leaving every request with an expired JWT (PostgREST 401
PGRST303) after the app returns to the foreground - until the user signs out and back in. Hit this in production: the edge logs for the incident show a burst of 401s on every data request and not a singlegrant_type=refresh_tokencall.What this does
Mirrors the Android implementation via
NSNotificationCenter:UIApplicationDidEnterBackgroundNotification: cancel auto refresh and set the status toInitializing, as Android'sonStopdoes.UIApplicationWillEnterForegroundNotification: re-load the session from storage, which runs the existing threshold check - refreshing immediately if the token went stale while suspended, or re-arming the timer with the correct remaining time if not.Gated behind the existing
enableLifecycleCallbacksconfig flag (its doc now says Android, iOS and tvOS).Structure
The
appleMainsetupPlatformactual now delegates to a per-familysetupApplePlatformexpect:uikitintermediate source set (iOS + tvOS, added to the hierarchy inAuth/build.gradle.kts) holds the observer registration;initDone()-only behavior - processes are not suspended there, so the timer keeps running;WKApplication), not UIKit, so it would need its own implementation.Notes
enableLifecycleCallbacks = false.:auth-kt:compileKotlinIosArm64,compileKotlinIosSimulatorArm64,compileKotlinTvosArm64,compileKotlinWatchosArm64,compileKotlinMacosArm64,compileKotlinJvm,allMetadataJar.