Skip to content

homework_2: done #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

krus210
Copy link

@krus210 krus210 commented Apr 19, 2021

No description provided.

CrashMonitor.trackWarning()
fun loadFactAndImage() {
presenterScope.launch {
refreshJob?.cancelAndJoin()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А зачем здесь cancelAndJoin?

_catsView?.populate(response.body()!!)
presenterScope.launch {
factJob?.cancelAndJoin()
factJob = launch {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему решил создать еще одну корутину с таким же контекстом, а контекст переключил внутри? Я думаю достаточно просто переключения через withContext

}
_catsView?.populate(result)
} catch (ex: SocketTimeoutException) {
_catsView?.showErrorDialog(ex.localizedMessage ?: "error")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно в одном catch использовать when блок с проверкой типа исключения

fun loadFactAndImage() {
presenterScope.launch {
refreshJob?.cancelAndJoin()
refreshJob = launch {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нужна ли здесь эта корутина? Думаю можно ее опустить и сразу внутри корутины запущенной на presenterScope запускать 2 параллельные через async

@@ -20,5 +20,6 @@ data class Fact(
@field:SerializedName("user")
val user: String,
@field:SerializedName("updatedAt")
val updatedAt: String
val updatedAt: String,
val image: String = ""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не совсем по теме ДЗ, но я бы советовал делать отдельные 2 ДТО на респонсы и их мержить в одну которая пойдет в другой слой

private val imageService: ImageService
) : ViewModel() {

val resultLiveData = MutableLiveData<Result>()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не по теме ДЗ, но наружу должна торчать имутабельная LiveData, чтобы эмитить в нее мог только 1 источник

}

fun onInitComplete() {
viewModelScope.launch(Dispatchers.IO + handler) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тебе здесь можно не переключать контекст. Достаточно переключить в withContext, иначе если бы у тебя не было метода postValue который в любом случае процессится на мейн треде, у тебя была бы ошибка


fun onInitComplete() {
viewModelScope.launch(Dispatchers.IO + handler) {
factJob?.cancelAndJoin()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно убрать


fun loadFactAndImage() {
viewModelScope.launch(Dispatchers.IO + handler) {
refreshJob?.cancelAndJoin()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно убрать

fun loadFactAndImage() {
viewModelScope.launch(Dispatchers.IO + handler) {
refreshJob?.cancelAndJoin()
refreshJob = launch {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лишняя корутина

@krus210 krus210 closed this May 12, 2021
Infernno pushed a commit to Infernno/Coroutines-Homework that referenced this pull request Oct 17, 2021
PavelScherbakov referenced this pull request in PavelScherbakov/Coroutines-Homework Jul 15, 2022
timma added a commit to timma/Coroutines-Homework that referenced this pull request Sep 15, 2022
temikng pushed a commit to temikng/Coroutines-Homework that referenced this pull request Apr 14, 2024
firkatdavletov added a commit to firkatdavletov/Coroutines-Homework that referenced this pull request Apr 15, 2024
1. Изменен возвращаемый тип в `CatsService` и добавлен модификатор `suspend`
2. Переписана логика в презентере с `Callback` на корутины и `suspend` функции
3. Реализован свой скоуп: PresenterScope с `MainDispatcher` и CoroutineName("CatsCoroutine") в качестве элементов контекста
4. Добавлена обработка исключений через try-catch. В случае `java.net.SocketTimeoutException` показываем Toast с текстом "Не удалось получить ответ от сервером". В остальных случаях логируем исключение в `otus.homework.coroutines.CrashMonitor` и показываем Toast с `exception.message`
temikng pushed a commit to temikng/Coroutines-Homework that referenced this pull request May 5, 2024
AkashevPavel added a commit to AkashevPavel/Coroutines-Homework that referenced this pull request Apr 23, 2025
AkashevPavel added a commit to AkashevPavel/Coroutines-Homework that referenced this pull request Apr 23, 2025
AkashevPavel added a commit to AkashevPavel/Coroutines-Homework that referenced this pull request Apr 23, 2025
AkashevPavel added a commit to AkashevPavel/Coroutines-Homework that referenced this pull request Apr 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants