Package 

Class CompletionAwaiter


  • 
    public final class CompletionAwaiter
    
                        

    A 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()

    • Method Summary

      Modifier and Type Method Description
      final Unit complete() Completes the awaiter, unblocking both blocking and suspend callers.
      final Boolean await(Long timeoutMs) Wait for completion using blocking approach with an optional timeout.
      final Unit awaitSuspend() Wait for completion using suspend approach (non-blocking for coroutines).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CompletionAwaiter

        CompletionAwaiter(String componentName)
    • Method Detail

      • complete

         final Unit complete()

        Completes the awaiter, unblocking both blocking and suspend callers.

      • 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.