Skip to content

Homework done #2

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

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open

Homework done #2

wants to merge 5 commits into from

Conversation

101b1
Copy link

@101b1 101b1 commented Apr 29, 2021

3 subtasks done, 1 commit per each.
Boris Lunyakov

}

override fun onFailure(call: Call<Fact>, t: Throwable) {
catsJob?.cancel()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Я думаю т.к этот метод вызывается в onCreate здесь cancel можно убрать. Т.к если активити пересоздастся то у тебя в любом случае будет другой инстанс презентера и Job

Copy link
Author

Choose a reason for hiding this comment

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

onInitComplete еще вызывается по нажатию кнопки, таким образом мы запускаем новую корутину не останавливая старую. Верно?


override fun onFailure(call: Call<Fact>, t: Throwable) {
catsJob?.cancel()
catsJob = PresenterScope().launch {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Лучше не создавать каждый раз новый инстанс, и переместить его в тело класса. И отменять не джобу, а сразу скоуп

Copy link
Author

Choose a reason for hiding this comment

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

Исправил это и последующие замечания, все в последнем коммите.

val fact = async { catsService.getCatFact() }
val image = async { imageService.getRandomImage()}
_catsView?.populate(CatsModel(fact.await(), image.await()))
} catch (socketEx: SocketTimeoutException){
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 блок и проверять по типу эксепшена.


private val catInfo: MutableLiveData<Result> = MutableLiveData<Result>()
private val exceptionHandler = CoroutineExceptionHandler() { _, exception ->
catInfo.value = Error(exception.localizedMessage ?: "An error occurred")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Наверное непонятно описал условия. Нужно обработать java.net.SocketTimeoutExceptionэксепшен и вернуть Result.Error(message), а в случае другого типа эксепшена вызвать CrashMonitor

try {
val fact = async { handleResponse(catsService.getCatFact())
}
val image = async { handleResponse(imageService.getRandomImage()) }
Copy link

Choose a reason for hiding this comment

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

val fact = catsScope.async { handleResponse(catsService.getCatFact())  }
val image = catsScope.async { handleResponse(imageService.getRandomImage()) }

иначе при отсутствии интернета будет креш

Infernno pushed a commit to Infernno/Coroutines-Homework that referenced this pull request Oct 17, 2021
PavelScherbakov added a commit to PavelScherbakov/Coroutines-Homework that referenced this pull request Jul 15, 2022
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.

3 participants