Skip to content

Commit

Permalink
Update task.md
Browse files Browse the repository at this point in the history
language checked
  • Loading branch information
stephen-hero authored Dec 12, 2023
1 parent a5f9de7 commit ca58565
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions Coroutines/Retrofit callback API/task.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
In the previous solution, the whole loading logic is moved to the background thread, but that still isn't the best use of
resources. All of the loading requests go sequentially and the thread is blocked while waiting for the loading result,
while it could have been occupied by other tasks. Specifically, the thread could start loading another request to
In the previous solution, the whole loading logic was moved to a background thread. However, this may not be the best use of
resources. The issue is that all of the loading requests are going sequentially, blocking the thread while waiting for the loading result.
The thread could, instead, be occupied by other tasks. Specifically, the thread could start loading another request and thus
receive the entire result earlier.

Handling the data for each repository should then be divided into two parts: loading and processing the
resulting response. The second _processing_ part should be extracted into a callback.
The data handling for each repository should be divided into two parts: loading and processing the
resultant response. The latter _processing_ part should be extracted into a callback.

The loading for each repository can then be started before the result for the previous repository is received (and the
corresponding callback is called):
The loading for each repository can then start before the result for the previous repository is received and before the
corresponding callback is called:

![Using callback API](images/callbacks.png)

The Retrofit callback API can help achieve this. The `Call.enqueue()` function starts an HTTP request and takes a
callback as an argument. In this callback, you need to specify what needs to be done after each request.
callback as an argument. In this callback, you must specify what needs to be done after each request.

Open [src/tasks/Request3Callbacks.kt](course://Coroutines/Retrofit callback API/src/tasks/Request3Callbacks.kt) and see the implementation of `loadContributorsCallbacks()` that uses this API.

* For convenience, this code fragment uses the `onResponse()` extension function declared in the same file. It takes a
* For convenience, this code fragment uses the `onResponse()` extension function declared in the same file. This function takes a
lambda as an argument rather than an object expression.
* The logic for handling the responses is extracted into callbacks: the corresponding lambdas start at lines `#1` and `#2`.
* The logic to handle the responses is extracted into callbacks: the corresponding lambdas start at lines `#1` and `#2`.

However, the provided solution doesn't work. If you run the program and load contributors by choosing the _CALLBACKS_
option, you'll see that nothing is shown. However, the tests that immediately return the result pass.
option, you'll see that nothing is displayed. However, the tests that immediately return the result pass.

## Task

Rewrite the code in the [src/tasks/Request3Callbacks.kt](course://Coroutines/Retrofit callback API/src/tasks/Request3Callbacks.kt) file so that the loaded list of contributors is shown.
Rewrite the code in the [src/tasks/Request3Callbacks.kt](course://Coroutines/Retrofit callback API/src/tasks/Request3Callbacks.kt) file so that the loaded list of contributors is displayed.

For a more detailed description, you can look at [this article](https://kotlinlang.org/docs/coroutines-and-channels.html#use-the-retrofit-callback-api)
For a more detailed description, you can refer to [this article](https://kotlinlang.org/docs/coroutines-and-channels.html#use-the-retrofit-callback-api).

0 comments on commit ca58565

Please sign in to comment.