https://kotlinlang.org logo
#coroutines
Title
# coroutines
o

otakusenpai

10/19/2018, 7:55 AM
@SUPERCILEX I can't delay inside the main thread because it should wait for 1 minute, which is a long time for the app(its networked)
o

oday

10/19/2018, 7:55 AM
like this here
Copy code
override fun fetchAndSaveFavorites(): Observable<List<WishResponse>> =
        carsService.getFavorites().map { favorites ->
            database.favoritesDao().wipeTable()
            database.favoritesDao().insert(favorites)
            favorites
        }
insert now accepts List<FavoriteCars> not List<WishResponse> anymore, but the data required to make a FavoriteCar is inside each WishResponse
e

elizarov

10/19/2018, 7:55 AM
Copy code
launch { 
    delay(60000)
    // do something after 1 minute
}
o

otakusenpai

10/19/2018, 7:57 AM
but then i could launch a new coroutine in each loop of the while loop
which could result in a cascading effect
e

elizarov

10/19/2018, 7:59 AM
Copy code
launch {
    while(true) {
        delay(60000)
        // do something _every_ minute
    }
}
s

SUPERCILEX

10/19/2018, 8:00 AM
@elizarov nooooooo
while(true)
is bad. Should be
while(isActive)
so you don't shoot yourself in the foot later cancelling stuff
Or does
delay
actually handle that for you?
e

elizarov

10/19/2018, 8:01 AM
No bad.
delay
is cancellable. You can do
while(isActive)
but you don’t have to.
s

SUPERCILEX

10/19/2018, 8:02 AM
Damn, that's sweet! TIL 😁
3 Views