Skip to content
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

Persist the useragent string across app starts. Make the class thread safe #1

Closed
wants to merge 5 commits into from

Conversation

weibel
Copy link

@weibel weibel commented May 29, 2024

This PR ensures the user agent is retrieved efficiently, stored persistently, and accessed in a thread-safe manner.

Key Features:

  • Singleton Pattern: Ensures a single instance of UserAgentService is used throughout the application.
  • Thread Safety: Utilizes a DispatchSemaphore to synchronize access to the user agent string.
  • Persistent Storage: Stores the user agent string in UserDefaults, keyed by the current OS version, to minimize fetch time on subsequent app launches.
  • User Agent Retrieval: Fetches the user agent string using a WKWebView, with an added delay to ensure the web view is ready for use.
  • Extension for Persistence: Encapsulates the logic for loading and saving the user agent string from/to UserDefaults in an extension.

Implementation Details:

  • The userAgent property provides thread-safe access to the user agent string.
  • On initialization, the service attempts to load the user agent from UserDefaults. If not found, it fetches it using a WKWebView.
  • The fetchUserAgent method includes a delay to ensure the WKWebView is ready and handles errors gracefully.
  • An extension is used to manage persistence, including methods for loading, saving, and resetting the user agent string in UserDefaults.

@weibel
Copy link
Author

weibel commented Jun 5, 2024

The threading issue has been solved with prebid#989
The persistence is proposed in this prebid#992

@weibel weibel closed this Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant