Giter Site home page Giter Site logo

lukaslechnerdev / kotlin-coroutines-and-flow-usecases-on-android Goto Github PK

View Code? Open in Web Editor NEW
2.6K 85.0 399.0 2.36 MB

๐ŸŽ“ Learning Kotlin Coroutines and Flows for Android by example. ๐Ÿš€ Sample implementations for real-world Android use cases. ๐Ÿ›  Unit tests included!

License: Apache License 2.0

Kotlin 100.00%
android kotlin-coroutines kotlin kotlin-flow coroutines flow kotlin-channels channels reactive

kotlin-coroutines-and-flow-usecases-on-android's People

Contributors

lukaslechnerdev avatar stkent avatar thesohelshaikh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kotlin-coroutines-and-flow-usecases-on-android's Issues

Main or IO thread

According to this code your network request will be made in MainThread. Isn't it?

`class PerformSingleNetworkRequestViewModel(
private val mockApi: MockApi = mockApi()
) : BaseViewModel() {

fun performSingleNetworkRequest() {
    uiState.value = UiState.Loading
    viewModelScope.launch {
        try {
            val recentAndroidVersions = mockApi.getRecentAndroidVersions()
            uiState.value = UiState.Success(recentAndroidVersions)
        } catch (exception: Exception) {
            Timber.e(exception)
            uiState.value = UiState.Error("Network Request failed!")
        }
    }
}}`

Maybe need to change it a little bit to launch(Dispatchers.IO) {... and uiState.postValue(...)

Perform several network requests concurrently

coroutineScope {
                    val oreoFeaturesDeferred = async { mockApi.getAndroidVersionFeatures(27) }
                    val pieFeaturesDeferred = async { mockApi.getAndroidVersionFeatures(28) }
                    val android10FeaturesDeferred = async { mockApi.getAndroidVersionFeatures(29) }

                    val oreoFeatures = oreoFeaturesDeferred.await()
                    val pieFeatures = pieFeaturesDeferred.await()
                    val android10Features = android10FeaturesDeferred.await()

                    val versionFeatures = listOf(oreoFeatures, pieFeatures, android10Features)

                    // other alternative: (but slightly different behavior when a deferred fails, see docs)
                    // val versionFeatures = awaitAll(oreoFeaturesDeferred, pieFeaturesDeferred, android10FeaturesDeferred)

                    uiState.value = UiState.Success(versionFeatures)
                }

i have some question about this code, deferred's await() function will suspend this coroutines, so this demo code can't performed in parallel

Typo in name of test function

https://github.com/LukasLechnerDev/Kotlin-Coroutine-Use-Cases-on-Android/blob/master/app/src/test/java/com/lukaslechner/coroutineusecasesonandroid/usecases/coroutines/usecase4/VariableAmountOfNetworkRequestsViewModelTest.kt
Line 108.
Name of the function should be:
performNetworkRequestsConcurrently() should return Success UiState on successful network requests after 2000ms
instead of:
performNetworkRequestsConcurrently() should return Error UiState on successful network requests after 2000ms

usecase4/VariableAmountOfNetworkRequestsViewModel#performNetworkRequestsConcurrently crashes on exception

Hi, Lukas!

I think there's a problem with the solution for

usecase4/VariableAmountOfNetworkRequestsViewModel#performNetworkRequestsConcurrently.

It crashes when I configure MockApi to return a 500 on the call to http://localhost/android-version-features/28.

Reading this about exceptions, it seems like try-catch will not catch the exception because async is not the direct child of the scope - launch is. So async will propagate the exception up to its parent (launch) and launch will throw the exception.

What do you think?

- Julian

Add info about Workmanager gotcha

Here's something that caught me out while using WorkManager with coroutines that you might want to add: Do not call a suspend function in the catch block (when you are trying to set the action as failed in the database etc). It will not be called when the work is cancelled due to system constraints. In that case WorkManager will throw JobCancellationException which means suspend functions will re-throw immediately. If you really want the suspend function to run, you must use NonCancellable context.
See: https://kotlinlang.org/docs/reference/coroutines/cancellation-and-timeouts.html#run-non-cancellable-block

Propagating state

Please how best can propagate state(like status) from the repository down to viewmodel using coroutine(no livedata usage in repository)?.
I read a medium article about livedata usage in any other layers aside presentation being inappropriate

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.