getInitializationState
@JvmStatic
@JvmName(name = "getInitializationState")
This suspend function observes the SDK's initialization state and pauses the calling coroutine until the process finishes with either a success or failure state. It is useful when an action in your application depends on the SDK being fully initialized.
Can be used to check the state of either automatic or manual initialization.
Example
lifecycleScope.launch {
when (val state = InPersonPaymentsTools.getInitializationState()){
InitializationState.SuccessfulInitialization -> {
// SDK is ready. You can now safely proceed.
}
is InitializationState.FailedInitialization -> {
// Handle initialization failure. Log errors or notify the user.
Log.e("MyApp", "SDK initialization failed: ${state.failureReasons}")
}
}
}
Content copied to clipboard
Handling Timeouts
The SDK has internal timeout for the initialization process, which means that this method will never suspend indefinitely. If a precise control over the waiting time is needed, wrap the call in a withTimeout
block. This throws a TimeoutCancellationException
if the operation does not complete within the specified duration, which you can catch to handle the timeout gracefully.
lifecycleScope.launch {
try {
// Wait for a maximum of 10 seconds.
withTimeout(10_000L) {
when( val state = InPersonPaymentsTools.getInitializationState() ){
InitializationState.SuccessfulInitialization -> {
// SDK is ready. You can now safely proceed.
}
is InitializationState.FailedInitialization -> {
// Handle initialization failure. Log errors or notify the user.
Log.e("MyApp", "SDK initialization failed: ${state.failureReasons}")
}
}
}
} catch (e: TimeoutCancellationException) {
// Handle the timeout scenario.
Log.e("MyApp", "SDK initialization timed out after 10 seconds.")
}
}
Content copied to clipboard