fun <T> Flow<T>.retry(retries: Long = Long.MAX_VALUE, predicate: suspend (cause: Throwable) -> Boolean = { true }): Flow<T>(source)

Retries collection of the given flow up to retries times when an exception that matches the given predicate occurs in the upstream flow. This operator is transparent to exceptions that occur in downstream flow and does not retry on exceptions that are thrown to cancel the flow.

See catch for details on how exceptions are caught in flows.

The default value of retries parameter is Long.MAX_VALUE. This value effectively means to retry forever. This operator is a shorthand for the following code (see retryWhen). Note that attempt is checked first and predicate is not called when it reaches the given number of retries:

retryWhen { cause, attempt -> attempt < retries && predicate(cause) }

The predicate parameter is always true by default. The predicate is a suspending function, so it can be also used to introduce delay before retry, for example:

flow.retry(3) { e ->
// retry on any IOException but also introduce delay if retrying
(e is IOException).also { if (it) delay(1000) }


when retries is not positive.