title: Android Kotlin Coroutines - Call Api Every X Seconds on Background
body: <p>I've build clean architectured app (with mvvm, use cases, compose). I've a <code>CoinListViewModel</code> for list all crypto coins by using <a href="https://coinpaprika.com/en/api/" rel="nofollow noreferrer">CoinPaprika API</a>. It is like;</p>
  <pre><code>@HiltViewModel
  class CoinListViewModel @Inject constructor(
      private val getCoinsUseCase: GetCoinsUseCase
  ) : ViewModel() {
  
      private val _state = mutableStateOf(CoinListState())
      val state: State&lt;CoinListState&gt; = _state
  
      init {
          getCoins()
      }
  
      private fun getCoins() {
          getCoinsUseCase().onEach { result -&gt;
              when (result) {
                  is Resource.Success -&gt; {
                      _state.value = CoinListState(coins = result.data ?: emptyList())
                  }
                  is Resource.Error -&gt; {
                      _state.value = CoinListState(
                          error = result.message ?: &quot;An unexpected error occured&quot;
                      )
                  }
                  is Resource.Loading -&gt; {
                      _state.value = CoinListState(isLoading = true)
                  }
              }
          }.launchIn(viewModelScope)
      }
  }
  </code></pre>
  <p>And this viewmodel is used in my <code>CoinListScreen</code> like;</p>
  <pre><code>
  @Composable
  fun CoinListScreen(
      navController: NavController,
      viewModel: CoinListViewModel = hiltViewModel()
  ) {
      val state = viewModel.state.value
      Box(modifier = Modifier.fillMaxSize()) {
          LazyColumn(modifier = Modifier.fillMaxSize()) {
              items(state.coins) { coin -&gt;
                  CoinListItem(
                      coin = coin,
                      onItemClick = {
                          navController.navigate(Screen.CoinDetailScreen.route + &quot;/${coin.id}&quot;)
                      }
                  )
              }
          }
          if(state.error.isNotBlank()) {
              Text(
                  text = state.error,
                  color = MaterialTheme.colors.error,
                  textAlign = TextAlign.Center,
                  modifier = Modifier
                      .fillMaxWidth()
                      .padding(horizontal = 20.dp)
                      .align(Alignment.Center)
              )
          }
          if(state.isLoading) {
              CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
          }
      }
  }
  </code></pre>
  <p>And that is my <code>GetCoinsUseCase</code>:</p>
  <pre><code>class GetCoinsUseCase @Inject constructor(
      private val repository: CoinRepository
  ) {
      operator fun invoke(): Flow&lt;Resource&lt;List&lt;Coin&gt;&gt;&gt; = flow {
          try {
              emit(Resource.Loading&lt;List&lt;Coin&gt;&gt;())
              val coins = repository.getCoins().map { it.toCoin() }
              emit(Resource.Success&lt;List&lt;Coin&gt;&gt;(coins))
  
          } catch(e: HttpException) {
              emit(Resource.Error&lt;List&lt;Coin&gt;&gt;(e.localizedMessage ?: &quot;An unexpected error occured&quot;))
          } catch(e: IOException){
              emit(Resource.Error&lt;List&lt;Coin&gt;&gt;(&quot;Couldn't reach to server. Check your internet connection.&quot;))
          }
      }
  }
  </code></pre>
  <p>I have 2 questions;</p>
  <ol>
  <li>How can I make this API call every 3 seconds ?</li>
  <li>How can I continue to do API call on background in phone ?</li>
  </ol>