-
Notifications
You must be signed in to change notification settings - Fork 239
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
Conversation
CrashMonitor.trackWarning() | ||
fun loadFactAndImage() { | ||
presenterScope.launch { | ||
refreshJob?.cancelAndJoin() |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 = "" |
There was a problem hiding this comment.
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>() |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лишняя корутина
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`
No description provided.