Skip to content

Move SharedPreferences usage to background thread #4480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 21, 2022

Conversation

lfkellogg
Copy link
Contributor

Followup from #4350

Creating a SharedPreferences object can touch storage, so we should do so only on a background thread.

@google-oss-bot
Copy link
Contributor

1 Warning
⚠️ Did you forget to add a changelog entry? (Add the 'no-changelog' label to the PR to silence this warning.)

Generated by 🚫 Danger

@lfkellogg lfkellogg force-pushed the lk/shared-prefs-to-background branch from 337977f to 0a76632 Compare December 20, 2022 22:20
Base automatically changed from lk/migrate-iaf-executors to fad/in-app-feedback December 20, 2022 22:23
@lfkellogg lfkellogg force-pushed the lk/shared-prefs-to-background branch from 0a76632 to a4b694f Compare December 20, 2022 22:24
return lifecycleNotifier
.applyToForegroundActivityTask(this::showSignInConfirmationDialog)
.onSuccessTask(lightweightExecutor, unused -> signInTester());
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe extract a method named something like signInIfNeeded() or ensureSignedIn() which encapsulates this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, this seems like TesterSignInManager.signInTester() - could that be called from here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TesterSignInManager.signInTester() opens the actual sign in screen in the browser.

Agreed that it would be nice to extract this though, if only to make this method more readable. I called it signInWithConfirmationIfNeeded().

return aabUpdater.updateAab(release);
} else {
return apkUpdater.updateApk(release, showDownloadInNotificationManager);
"Tester is not signed in", AUTHENTICATION_FAILURE));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this use assertTesterIsSignedIn()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh actually yes! It's a little tricky because I have to use TaskUtils.onSuccessUpdateTask() but it works.

}

boolean getSignInStatus() {
return signInSharedPreferences.get().getBoolean(SIGNIN_TAG, false);
private SharedPreferences getSharedPreferencesBlocking() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comment explaining that this might involve I/O

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
}
});
executor.awaitTermination(100, TimeUnit.MILLISECONDS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't 100 ms too short? The thread sleeps 100 ms for every iteration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, true. Adjusted to 50ms sleeps and a 500ms timeout.

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Dec 20, 2022

Coverage Report 1

Affected Products

  • firebase-appdistribution

    Overall coverage changed from ? (8a08968) to 76.53% (6c18c4c) by ?.

    42 individual files with coverage change

    FilenameBase (8a08968)Merge (6c18c4c)Diff
    AabUpdater.java?98.67%?
    ApkInstaller.java?96.88%?
    ApkUpdater.java?94.44%?
    AppDistributionReleaseImpl.java?100.00%?
    AppDistributionReleaseInternal.java?100.00%?
    AppIconSource.java?84.62%?
    AutoValue_AppDistributionReleaseImpl.java?65.45%?
    AutoValue_AppDistributionReleaseInternal.java?71.58%?
    AutoValue_ImageUtils_ImageSize.java?35.00%?
    AutoValue_TesterApiDisabledErrorDetails.java?29.41%?
    AutoValue_TesterApiDisabledErrorDetails_HelpLink.java?54.17%?
    AutoValue_UpdateProgressImpl.java?65.96%?
    ErrorMessages.java?0.00%?
    FeedbackActivity.java?1.12%?
    FeedbackSender.java?84.62%?
    FirebaseAppDistributionExceptions.java?80.00%?
    FirebaseAppDistributionFileProvider.java?0.00%?
    FirebaseAppDistributionImpl.java?89.67%?
    FirebaseAppDistributionLifecycleNotifier.java?92.91%?
    FirebaseAppDistributionNotificationsManager.java?87.18%?
    FirebaseAppDistributionRegistrar.java?86.27%?
    FirebaseAppDistributionTesterApiClient.java?88.54%?
    HttpsUrlConnectionFactory.java?50.00%?
    ImageUtils.java?100.00%?
    InstallActivity.java?2.67%?
    LogWrapper.java?100.00%?
    NewReleaseFetcher.java?80.85%?
    PackageInfoUtils.java?42.86%?
    ReleaseIdentifier.java?85.54%?
    ReleaseUtils.java?83.33%?
    ScreenshotTaker.java?41.18%?
    SequentialReference.java?100.00%?
    SignInResultActivity.java?0.00%?
    SignInStorage.java?23.53%?
    TakeScreenshotAndStartFeedbackActivity.java?0.00%?
    TaskCache.java?100.00%?
    TaskUtils.java?91.67%?
    TesterApiDisabledErrorDetails.java?93.75%?
    TesterApiHttpClient.java?90.18%?
    TesterSignInManager.java?96.00%?
    UpdateProgressImpl.java?100.00%?
    UpdateTaskImpl.java?76.00%?

Test Logs

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/nwsoyaMlym.html

@github-actions
Copy link
Contributor

github-actions bot commented Dec 20, 2022

Unit Test Results

162 tests   162 ✔️  42s ⏱️
  16 suites      0 💤
  16 files        0

Results for commit b5aea47.

♻️ This comment has been updated with latest results.

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Dec 20, 2022

Size Report 1

Affected Products

  • base

    TypeBase (8a08968)Merge (6c18c4c)Diff
    apk (aggressive)?8.39 kB? (?)
    apk (release)?8.65 kB? (?)
  • firebase-annotations

    TypeBase (8a08968)Merge (6c18c4c)Diff
    apk (aggressive)?8.39 kB? (?)
    apk (release)?9.46 kB? (?)
  • firebase-appdistribution

    TypeBase (8a08968)Merge (6c18c4c)Diff
    aar?151 kB? (?)
    apk (aggressive)?832 kB? (?)
    apk (release)?2.01 MB? (?)
  • firebase-appdistribution-api

    TypeBase (8a08968)Merge (6c18c4c)Diff
    aar?16.0 kB? (?)
    apk (aggressive)?95.8 kB? (?)
    apk (release)?706 kB? (?)
  • firebase-common

    TypeBase (8a08968)Merge (6c18c4c)Diff
    aar?67.4 kB? (?)
    apk (aggressive)?95.1 kB? (?)
    apk (release)?700 kB? (?)
  • firebase-components

    TypeBase (8a08968)Merge (6c18c4c)Diff
    aar?44.9 kB? (?)
    apk (aggressive)?8.68 kB? (?)
    apk (release)?33.6 kB? (?)
  • firebase-installations

    TypeBase (8a08968)Merge (6c18c4c)Diff
    aar?55.0 kB? (?)
    apk (aggressive)?96.6 kB? (?)
    apk (release)?724 kB? (?)
  • firebase-installations-interop

    TypeBase (8a08968)Merge (6c18c4c)Diff
    aar?8.05 kB? (?)
    apk (aggressive)?65.0 kB? (?)
    apk (release)?651 kB? (?)

Test Logs

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/vNhfKyJjzl.html

@google-oss-bot
Copy link
Contributor

Startup Time Report 1

Note: Layout is sometimes suboptimal due to limited formatting support on GitHub. Please check this report on GCS.

Startup time comparison between the CI merge commit (6c18c4c) and the base commit (8a08968) are not available.

No macrobenchmark data found for the base commit (8a08968). Analysis for the CI merge commit (6c18c4c) can be found at:

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/KfBqKr8xF7/index.html

@lfkellogg lfkellogg merged commit 39a87fa into fad/in-app-feedback Dec 21, 2022
@lfkellogg lfkellogg deleted the lk/shared-prefs-to-background branch December 21, 2022 14:03
lfkellogg added a commit that referenced this pull request Jan 5, 2023
* Migrate executors in fad/in-app-feedback

* Add an import and format

* Move SharedPreferences usage to background thread

* Address feedback

* Fix onSuccessUpdateTask
@firebase firebase locked and limited conversation to collaborators Jan 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants