Skip to content

Latest commit

 

History

History
234 lines (196 loc) · 3.93 KB

101_why.md

File metadata and controls

234 lines (196 loc) · 3.93 KB
fun onCreate() {
    val news = getNewsFromApi()
    val sortedNews = news
        .sortedByDescending { it.publishedAt }
    view.showNews(sortedNews)
}
fun onCreate() {
  thread {
      val news = getNewsFromApi()
      val sortedNews = news
          .sortedByDescending { it.publishedAt }
      runOnUiThread {
          view.showNews(sortedNews)
      }
  }
}
fun onCreate() {
  getNewsFromApi { news ->
      val sortedNews = news
          .sortedByDescending { it.publishedAt }
      view.showNews(sortedNews)
  }
}
fun onCreate() {
  startedCallbacks += getNewsFromApi { news ->
      val sortedNews = news
          .sortedByDescending { it.publishedAt }
      view.showNews(sortedNews)
  }
}
fun showNews() {
  getConfigFromApi { config ->
      getNewsFromApi(config) { news ->
          getUserFromApi { user ->
              view.showNews(user, news)
          }
      }
  }
}
fun onCreate() {
  showProgressBar()
  showNews()
  hideProgressBar() // Wrong
}
fun onCreate() {
  showProgressBar()
  showNews {
      hideProgressBar()
  }
}
fun onCreate() {
  disposables += getNewsFromApi()
      .subscribeOn(Schedulers.io())
      .observeOn(AndroidSchedulers.mainThread())
      .map { news ->
          news.sortedByDescending { it.publishedAt }
      }
      .subscribe { sortedNews ->
          view.showNews(sortedNews)
      }
}
fun onCreate() {
  val news = getNewsFromApi()
  val sortedNews = news
      .sortedByDescending { it.publishedAt }
  view.showNews(sortedNews)
}
fun getNewsFromApi(): Single<List<News>>
fun showNews() {
  disposables += Observable.zip(
      getConfigFromApi().flatMap { getNewsFromApi(it) },
      getUserFromApi(),
      Function2 { news: List<News>, config: Config ->
          Pair(news, config)
      })
      .subscribeOn(Schedulers.io())
      .observeOn(AndroidSchedulers.mainThread())
      .subscribe { (news, config) ->
          view.showNews(news, config)
      }
}
fun onCreate() {
  viewModelScope.launch {
      val news = getNewsFromApi()
      val sortedNews = news
          .sortedByDescending { it.publishedAt }
      view.showNews(sortedNews)
  }
}
fun showNews() {
  viewModelScope.launch {
      val config = getConfigFromApi()
      val news = getNewsFromApi(config)
      val user = getUserFromApi()
      view.showNews(user, news)
  }
}
fun showNews() {
  viewModelScope.launch {
      val config = async { getConfigFromApi() }
      val news = async { getNewsFromApi(config.await()) }
      val user = async { getUserFromApi() }
      view.showNews(user.await(), news.await())
  }
}
// all pages will be loaded simultaneously
fun showAllNews() {
  viewModelScope.launch {
      val allNews = (0 until getNumberOfPages())
          .map { page -> async { getNewsFromApi(page) } }
          .flatMap { it.await() }
      view.showAllNews(allNews)
  }
}

// next pages are loaded one after another
fun showPagesFromFirst() {
  viewModelScope.launch {
      for (page in 0 until getNumberOfPages()) {
          val news = getNewsFromApi(page)
          view.showNextPage(news)
      }
  }
}
suspend fun getArticle(
  articleKey: String,
  lang: Language
): ArticleJson? {
  return articleRepository.getArticle(articleKey, lang)
      ?.let { toArticleJson(it) }
}

suspend fun getAllArticles(
  userUuid: String?,
  lang: Language
): List<ArticleJson> = coroutineScope {
  val user = async { userRepo.findUserByUUID(userUuid) }
  val articles = articleRepo.getArticles(lang)
  articles
      .filter { hasAccess(user.await(), it) }
      .map { toArticleJson(it) }
}
//1
import kotlin.concurrent.thread

fun main() {
  repeat(100_000) {
      thread {
          Thread.sleep(1000L)
          print(".")
      }
  }
}
//2
import kotlinx.coroutines.*

fun main() = runBlocking {
  repeat(100_000) {
      launch {
          delay(1000L)
          print(".")
      }
  }
}