-
public final class CompletionAwaiterA unified completion awaiter that supports both blocking and suspend-based waiting. This class allows both legacy blocking code and modern coroutines to wait for the same event.
It is designed for scenarios where certain tasks, such as SDK initialization, must finish before continuing. When used on the main/UI thread for blocking operations, it applies a shorter timeout and logs warnings to prevent ANR errors.
PERFORMANCE NOTE: Having both blocking (CountDownLatch) and suspend (Channel) mechanisms in place is very low cost and should not hurt performance. The overhead is minimal:
CountDownLatch: ~32 bytes, optimized for blocking threads
Channel: ~64 bytes, optimized for coroutine suspension
Total overhead: <100 bytes per awaiter instance
Notification cost: Two simple operations (countDown + trySend)
This dual approach provides optimal performance for each use case rather than forcing a one-size-fits-all solution that would be suboptimal for both scenarios.
Usage: val awaiter = CompletionAwaiter("OneSignal SDK Init")
// For blocking code: awaiter.await()
// For suspend code: awaiter.awaitSuspend()
// When complete: awaiter.complete()
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classCompletionAwaiter.Companion
-
Constructor Summary
Constructors Constructor Description CompletionAwaiter(String componentName)
-
Method Summary
Modifier and Type Method Description final Unitcomplete()Completes the awaiter, unblocking both blocking and suspend callers. final Booleanawait(Long timeoutMs)Wait for completion using blocking approach with an optional timeout. final UnitawaitSuspend()Wait for completion using suspend approach (non-blocking for coroutines). -
-
Constructor Detail
-
CompletionAwaiter
CompletionAwaiter(String componentName)
-
-
Method Detail
-
await
final Boolean await(Long timeoutMs)
Wait for completion using blocking approach with an optional timeout.
- Parameters:
timeoutMs- Timeout in milliseconds, defaults to context-appropriate timeout
-
awaitSuspend
final Unit awaitSuspend()
Wait for completion using suspend approach (non-blocking for coroutines). This method will suspend the current coroutine until completion is signaled.
-
-
-
-