-
Notifications
You must be signed in to change notification settings - Fork 376
feature: Exposing accessors thru suspend #2502
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
base: main
Are you sure you want to change the base?
Conversation
| prefs.edit().clear().commit() | ||
| val otherPrefs = context.getSharedPreferences("com.onesignal", Context.MODE_PRIVATE) | ||
| otherPrefs.edit().clear().commit() | ||
| Thread.sleep(50) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to explicitly wait here?
|
|
||
| context("suspend accessor methods exist and return correct types") { | ||
| test("getUserSuspend returns IUserManager after initialization") { | ||
| runBlocking { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we have runBlocking around every test? The suspend methods are already blocking until completion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
Description
One Line Summary
Expose suspend-safe accessor methods for all SDK managers and configuration properties to prevent ANRs and improve thread safety.
Details
This PR adds suspend-based accessor methods to the
OneSignalclass, providing non-blocking alternatives to the existing property accessors. These methods are designed to work seamlessly with Kotlin coroutines and prevent Application Not Responding (ANR) errors by ensuring all SDK operations run on background threads.New Suspend Accessor Methods
Manager Accessors:
getUserSuspend()- Suspend-safe version ofUserpropertygetSessionSuspend()- Suspend-safe version ofSessionpropertygetNotificationsSuspend()- Suspend-safe version ofNotificationspropertygetLocationSuspend()- Suspend-safe version ofLocationpropertygetInAppMessagesSuspend()- Suspend-safe version ofInAppMessagespropertyConfiguration Property Accessors:
getConsentRequiredSuspend()/setConsentRequiredSuspend()- Suspend-safe version ofconsentRequiredpropertygetConsentGivenSuspend()/setConsentGivenSuspend()- Suspend-safe version ofconsentGivenpropertygetDisableGMSMissingPromptSuspend()/setDisableGMSMissingPromptSuspend()- Suspend-safe version ofdisableGMSMissingPromptpropertyUser Management Methods:
loginSuspend()- Suspend-safe version oflogin()method (already existed, improved documentation)logoutSuspend()- Suspend-safe version oflogout()method (already existed, improved documentation)Key Benefits
Dispatchers.IO*Suspend) and behaviorUsage Example
in
// Before (can cause ANR if called on main thread before init completes)
val user = OneSignal.User
OneSignal.consentRequired = true
// After (safe to call from any thread, including main thread)
lifecycleScope.launch {
val user = OneSignal.getUserSuspend()
OneSignal.setConsentRequiredSuspend(true)
}### Motivation
Starting in 5.4.0, we've been moving SDK operations to background threads to reduce ANR reports. While the existing property accessors work, they can still block threads if the SDK isn't fully initialized. These suspend methods provide a cleaner, more explicit way for developers to use the SDK in coroutine-based code without worrying about thread blocking or ANRs.
The suspend methods automatically:
Dispatchers.IO)Scope
OneSignalclassIOneSignalinterface methods (no duplicate implementation)@JvmStaticannotation for Java interopTesting
Unit testing
Added comprehensive test suite (
OneSignalSuspendAccessorsTests.kt) that verifies:All existing tests continue to pass.
Manual testing
Tested on:
Verified that:
Affected code checklist
getNotificationsSuspend())getNotificationsSuspend())getNotificationsSuspend())getNotificationsSuspend())getUserSuspend())getSessionSuspend())getInAppMessagesSuspend())Checklist
Overview
Testing
Final pass
This change is