Skip to content

Commit da64183

Browse files
committed
Refine Coroutine wording around when to use suspend in method declarations.
Closes #2503
1 parent 13f9657 commit da64183

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/main/asciidoc/kotlin-coroutines.adoc

+9
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,13 @@ Coroutines repositories are built on reactive repositories to expose the non-blo
8080
Methods on a Coroutines repository can be backed either by a query method or a custom implementation.
8181
Invoking a custom implementation method propagates the Coroutines invocation to the actual implementation method if the custom method is `suspend`-able without requiring the implementation method to return a reactive type such as `Mono` or `Flux`.
8282

83+
Note that depending on the method declaration the coroutine context may or may not be available.
84+
To retain access to the context, either declare your method using `suspend` or return a type that enables context propagation such as `Flow`.
85+
86+
* `suspend fun findOne(id: String): User`: Retrieve the data once and synchronously by suspending.
87+
* `fun findByFirstname(firstname: String): Flow<User>`: Retrieve a stream of data.
88+
The `Flow` is created eagerly while data is fetched upon `Flow` interaction (`Flow.collect(…)`).
89+
* `fun getUser(): User`: Retrieve data once *blocking the thread* and without context propagation.
90+
This should be avoided.
91+
8392
NOTE: Coroutines repositories are only discovered when the repository extends the `CoroutineCrudRepository` interface.

0 commit comments

Comments
 (0)