{"id":291309,"date":"2018-10-24T21:05:02","date_gmt":"2018-10-24T17:05:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=291309"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2018-10-24T17:05:02","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=291309","title":{"rendered":"Android LiveData \u043d\u0430 Kotlin \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Retrofit \u0438 coroutines"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\u0421\u0442\u0430\u0442\u044c\u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043e\u0431 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <abbr title=\"Google Android Jetpack\">Android Components<\/abbr> ViewModel, LifeCycle \u0438 LiveData. \u042d\u0442\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043d\u0435 \u0437\u0430\u0431\u043e\u0442\u0438\u0442\u044c\u0441\u044f \u043e \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u043c \u0446\u0438\u043a\u043b\u0435 Activity.<br \/>  \u0422\u0430\u043a \u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e <abbr title=\"Coroutines support Concurrency in one thread\">Coroutines<\/abbr> \u0432 \u0441\u0432\u044f\u0437\u043a\u0435 \u0441 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u0430\u0440\u0438\u0435\u043c \u043d\u0430 Retrofit  <\/p>\n<pre><code>fun main(args: Array&lt;String&gt;): Unit = runBlocking {     \/\/ Wait (suspend) for Result     val result: Result&lt;User&gt; = api.getUser(\"username\").awaitResult()     \/\/ Check result type     when (result) {         \/\/Successful HTTP result         is Result.Ok -&gt; saveToDb(result.value)         \/\/ Any HTTP error         is Result.Error -&gt; log(\"HTTP error with code ${result.error.code()}\", result.error)         \/\/ Exception while request invocation         is Result.Exception -&gt; log(\"Something broken\", e)     } } <\/code><\/pre>\n<p>  <a name=\"habracut\"><\/a><\/p>\n<p>  <b>Retrofit coroutines extension<\/b><br \/>  <a href=\"https:\/\/github.com\/gildor\/kotlin-coroutines-retrofit\">kotlin-coroutines-retrofit<\/a><br \/>  \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0434\u043b\u044f Retrofit \u043d\u0430 Kotlin. \u042d\u0442\u043e \u0432\u0441\u0435\u0433\u043e \u0434\u0432\u0430 \u0444\u0430\u0439\u043b\u0430. \u042f \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u0438\u0445 \u0432 \u043f\u0440\u043e\u0435\u043a\u0442. \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0438\u0445 \u0447\u0435\u0440\u0435\u0437 Dependency \u0432 Gradle. \u041d\u0430 Github \u0435\u0441\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f.<br \/>  \u0422\u0430\u043a\u0436\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c Adapter <i>addCallAdapterFactory(CoroutineCallAdapterFactory())<\/i>.<br \/>  ServerAPI \u0438 Repository \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435<\/p>\n<p>  <b>REST API<\/b><br \/>  \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 REST API \u043d\u0430 Kotlin. \u041e\u043d\u0430 \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u043a\u0430\u043a\u0438\u0445 \u043b\u0438\u0431\u043e \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u043d\u044b\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">ServerAPI<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code>import android.arch.lifecycle.MutableLiveData import android.util.Log import com.jakewharton.retrofit2.adapter.kotlin.coroutines.experimental.CoroutineCallAdapterFactory import kotlinx.coroutines.experimental.android.UI import kotlinx.coroutines.experimental.async import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory import okhttp3.OkHttpClient import okhttp3.logging.HttpLoggingInterceptor import retrofit2.Call import retrofit2.http.* import ru.gildor.coroutines.retrofit.Result import ru.gildor.coroutines.retrofit.awaitResult  object ServerAPI {      var API_BASE_URL: String = getNetworkHost();     var httpClient = OkHttpClient.Builder().addInterceptor(         HttpLoggingInterceptor().apply {             level = if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY else HttpLoggingInterceptor.Level.NONE         })      var builder: Retrofit.Builder = Retrofit.Builder()             .baseUrl(API_BASE_URL)             .addCallAdapterFactory(CoroutineCallAdapterFactory())             .addConverterFactory(GsonConverterFactory.create())      var retrofit = builder             .client(httpClient.build())             .build()      var netService = retrofit.create&lt;NetService&gt;(         NetService::class.java!!)      interface NetService {         @GET(\"api\/stores\")         fun getStoreAll(@Header(\"Authorization\") bearer: String): Call&lt;Array&lt;Store&gt;&gt;     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <b>LiveData<\/b><br \/>  \u0414\u0430\u043b\u0435\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c Repository. \u042d\u0442\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0441\u0435\u0440\u0432\u0438\u0441 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f LiveData. \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c LiveData \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438: <i>Resource.loading(null)<\/i>. \u0414\u0430\u043b\u0435\u0435 \u043e\u0436\u0438\u0434\u0430\u0435\u043c \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 <i>awaitResult()<\/i> \u042d\u0442\u043e\u0442 \u0432\u044b\u0437\u043e\u0432 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0432 \u0431\u043b\u043e\u043a\u0435 Coroutin <i>async(UI)<\/i><br \/>  \u041f\u043e \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0445\u044d\u043d\u0434\u043b\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u0445\u043e\u0440\u043e\u0448\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d \u0432 <i>mutableLiveData.value = Resource.success(result.value)<\/i> \u0412\u0430\u0436\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u2014 \u044d\u0442\u043e \u0434\u043e\u043b\u0436\u0435\u043d\u0430 \u0431\u044b\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043d\u043e\u0432\u044b\u0439 \u044d\u043a\u0437\u044d\u043c\u043f\u043b\u044f\u0440, \u0438\u043d\u0430\u0447\u0435 observer LiveData \u043d\u0435 \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. see: <i>new Resource&lt;&gt;(SUCCESS, data, null);<\/i>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">Repository<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code>   class Repository {      fun getStores(token: String)  :  MutableLiveData&lt;Resource&lt;Array&lt;Store&gt;&gt;&gt;{         val mutableLiveData = MutableLiveData&lt;Resource&lt;Array&lt;Store&gt;&gt;&gt;()         mutableLiveData.value = Resource.loading(null)         val req = PostsAPI.netService.getStoreAll(token)          try {             async(UI) {                 val result = req.awaitResult()                 \/\/ Check result type                 when (result) {                     \/\/Successful HTTP result                     is Result.Ok -&gt; {                         mutableLiveData.value = Resource.success(result.value)                     }                     \/\/ Any HTTP error                     is Result.Error -&gt; {                         mutableLiveData.value  = Resource.error(\"Http Error!\", null)                     }                     \/\/ Exception while request invocation                     is Result.Exception -&gt; Log.d(TAG, result.exception.message)                 }             }         } catch (e: Exception) {             Log.d(TAG, e.toString())         }         return mutableLiveData     } }      <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <b>Wrapper data<\/b><br \/>  \u0414\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043e\u0448\u0438\u0431\u043e\u043a \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0432 Fragment \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f Wrapper \u2014 <i>Resource&lt;T&gt;<\/i>. <br \/>  \u041e\u043d \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0442\u0440\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f:  <\/p>\n<pre><code>public enum Status { SUCCESS, ERROR, LOADING }<\/code><\/pre>\n<p>  C\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0435:  <\/p>\n<pre><code>@Nullable public final T data;<\/code><\/pre>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">Resource&lt;T&gt;<\/b><\/p>\n<div class=\"spoiler_text\"><a href=\"https:\/\/developer.android.com\/jetpack\/docs\/guide#addendum\">A generic class that contains data and status about loading this data<\/a>   <\/p>\n<pre><code class=\"java\">\/\/ A generic class that contains data and status about loading this data. public class Resource&lt;T&gt; {     @NonNull public final Status status;     @Nullable public final T data;     @Nullable public final String message;     private Resource(@NonNull Status status, @Nullable T data,             @Nullable String message) {         this.status = status;         this.data = data;         this.message = message;     }      public static &lt;T&gt; Resource&lt;T&gt; success(@NonNull T data) {         return new Resource&lt;&gt;(Status.SUCCESS, data, null);     }      public static &lt;T&gt; Resource&lt;T&gt; error(String msg, @Nullable T data) {         return new Resource&lt;&gt;(Status.ERROR, data, msg);     }      public static &lt;T&gt; Resource&lt;T&gt; loading(@Nullable T data) {         return new Resource&lt;&gt;(Status.LOADING, data, null);     }      public enum Status { SUCCESS, ERROR, LOADING } }       <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <b>ViewModel<\/b><br \/>  StoresViewModel \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0443 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0432\u043e \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <i>stores<\/i>  <\/p>\n<pre><code>val api = Repository() stores = api.getStores(token) <\/code><\/pre>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">ViewModel<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code>class StoresViewModel (context: Context, token: String) : ViewModel() {     val stores: MutableLiveData&lt;Resource&lt;Array&lt;Store&gt;&gt;&gt;     init {         val api = Repository()         stores = api.getStores(token)     } }       <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <b>ViewModelProviders<\/b><br \/>  \u0414\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0432 ViewModel \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0443\u044e <i>ViewModelProviders<\/i><br \/>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0432 <i>LoginViewModel<\/i> \u043d\u0430\u0434\u043e \u0434\u0432\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 (Login,Password). \u0414\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0442\u043e\u043a\u0435\u043d\u0430 \u0432 <i>StoresViewModel<\/i> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u0434\u0438\u043d (Token)  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">AppViewModelFactory<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code>class AppViewModelFactory(private val contect: Context, vararg params: Any) :     ViewModelProvider.NewInstanceFactory() {     private val mParams: Array&lt;out Any&gt;      init {         mParams = params     }      override fun &lt;T : ViewModel&gt; create(modelClass: Class&lt;T&gt;): T {         return if (modelClass == LoginViewModel::class.java) {             LoginViewModel(contect, mParams[0] as String, mParams[1] as String) as T         } else if (modelClass == StoresViewModel::class.java) {             StoresViewModel(contect, mParams[0] as String) as T         } else {             super.create(modelClass)         }     } }       <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <b>Fragment<\/b><br \/>  \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 StoresViewModel:  <\/p>\n<pre><code>viewModel = ViewModelProviders.of(this, AppBuyViewModelFactory(requireActivity(), tokenHolder.token)).get(StoresViewModel::class.java) <\/code><\/pre>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u0435\u043b\u044f Observer \u0437\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445:  <\/p>\n<pre><code> \/\/ Observe data on the ViewModel, exposed as a LiveData  viewModel.stores.observe(this, Observer&lt;Resource&lt;Array&lt;Store&gt;&gt;&gt; { storesResource -&gt; <\/code><\/pre>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">Fragment<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code>    override fun onCreateView(         inflater: LayoutInflater, container: ViewGroup?,         savedInstanceState: Bundle?     ): View? {         val view = inflater.inflate(R.layout.stores_fragment, container, false)          val tokenHolder = TokenHolder(PreferenceManager.getDefaultSharedPreferences(requireActivity()))          viewModel = ViewModelProviders.of(this, AppViewModelFactory(requireActivity(), tokenHolder.token)).get(StoresViewModel::class.java)          recyclerView = view.findViewById&lt;RecyclerView&gt;(R.id.store_list).apply {             setHasFixedSize(true)         }         return view     }       override fun onActivityCreated(savedInstanceState: Bundle?) {         super.onActivityCreated(savedInstanceState)          \/\/ Observe data on the ViewModel, exposed as a LiveData         viewModel.stores.observe(this, Observer&lt;Resource&lt;Array&lt;Store&gt;&gt;&gt; { storesResource -&gt;              val stores = storesResource?.data             stores?.let {                 viewAdapter = StoresAdapter(stores!!)                 recyclerView.adapter = viewAdapter             }              if (storesResource?.status  == Resource.LOADING){                 log(\"Loading...\")             }              if (storesResource?.status  == Resource.ERROR){                 log(\"Error : \" + storesResource?.message)             }          })     }       <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  P.S.<\/p>\n<p>  \u0414\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f Token \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0435\u0433\u043e \u0432\u043e \u0432\u0441\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u044f \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043b \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443\/\u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u043e\u0442 Fabio Collini. \u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0445\u043e\u0440\u043e\u0448\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0432 \u0435\u0433\u043e \u0441\u0442\u0430\u0442\u044c\u0435. \u0421\u0441\u044b\u043b\u043a\u0430 \u0435\u0441\u0442\u044c \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u0432 Github \u0438\u043b\u0438 \u043d\u0438\u0436\u0435 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435.<br \/>  <a href=\"https:\/\/github.com\/NaluLabs\/prefs-delegates\">prefs-delegates by Fabio Collini<\/a><\/p>\n<pre><code>class TokenHolder(prefs: SharedPreferences) {     var token by prefs.string()         private set      var count by prefs.int()         private set      fun saveToken(newToken: String) {         token = newToken         count++     } } <\/code><\/pre>\n<p>  <b>Gradle<\/b>  <\/p>\n<pre><code>    implementation 'android.arch.lifecycle:extensions:1.1.1'     implementation \"org.jetbrains.kotlinx:kotlinx-coroutines-core:0.30.0\"     implementation \"org.jetbrains.kotlinx:kotlinx-coroutines-android:0.30.0\"     implementation \"com.squareup.retrofit2:retrofit:2.4.0\"     implementation \"com.squareup.retrofit2:converter-gson:2.4.0\"     implementation \"com.jakewharton.retrofit:retrofit2-kotlin-coroutines-experimental-adapter:1.0.0\"     \/\/ If you use Kotlin 1.2 or 1.3     \/\/ compile 'ru.gildor.coroutines:kotlin-coroutines-retrofit:0.13.0'     \/\/ compile 'ru.gildor.coroutines:kotlin-coroutines-retrofit:0.13.0-eap13' <\/code><\/pre>\n<p>  <b>Links<\/b><br \/>  \u0412\u0441\u0435 \u0432 \u043e\u0434\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435<br \/>  <a href=\"https:\/\/github.com\/googlesamples\/android-sunflower\">github.com\/googlesamples\/android-sunflower<\/a><\/p>\n<p>  Android Architecture Components samples<br \/>  <a href=\"https:\/\/github.com\/googlesamples\/android-architecture-components\">github.com\/googlesamples\/android-architecture-components<\/a><\/p>\n<p>  LiveData Overview<br \/>  <a href=\"https:\/\/developer.android.com\/topic\/libraries\/architecture\/livedata#kotlin\">developer.android.com\/topic\/libraries\/architecture\/livedata#kotlin<\/a><\/p>\n<p>  Async code using Kotlin Coroutines<br \/>  <a href=\"https:\/\/proandroiddev.com\/async-code-using-kotlin-coroutines-233d201099ff\">proandroiddev.com\/async-code-using-kotlin-coroutines-233d201099ff<\/a><\/p>\n<p>  Multithreading and Kotlin<br \/>  <a href=\"https:\/\/medium.com\/@korhanbircan\/multithreading-and-kotlin-ac28eed57fea\">medium.com\/@korhanbircan\/multithreading-and-kotlin-ac28eed57fea<\/a><\/div>\n<p>        <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d()}}}t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>     <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/post\/427475\/\"> https:\/\/habr.com\/post\/427475\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\u0421\u0442\u0430\u0442\u044c\u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043e\u0431 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <abbr title=\"Google Android Jetpack\">Android Components<\/abbr> ViewModel, LifeCycle \u0438 LiveData. \u042d\u0442\u0438 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043d\u0435 \u0437\u0430\u0431\u043e\u0442\u0438\u0442\u044c\u0441\u044f \u043e \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u043c \u0446\u0438\u043a\u043b\u0435 Activity.<br \/>  \u0422\u0430\u043a \u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e <abbr title=\"Coroutines support Concurrency in one thread\">Coroutines<\/abbr> \u0432 \u0441\u0432\u044f\u0437\u043a\u0435 \u0441 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u0430\u0440\u0438\u0435\u043c \u043d\u0430 Retrofit  <\/p>\n<pre><code>fun main(args: Array&lt;String&gt;): Unit = runBlocking {     \/\/ Wait (suspend) for Result     val result: Result&lt;User&gt; = api.getUser(\"username\").awaitResult()     \/\/ Check result type     when (result) {         \/\/Successful HTTP result         is Result.Ok -&gt; saveToDb(result.value)         \/\/ Any HTTP error         is Result.Error -&gt; log(\"HTTP error with code ${result.error.code()}\", result.error)         \/\/ Exception while request invocation         is Result.Exception -&gt; log(\"Something broken\", e)     } } <\/code><\/pre>\n<p>  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-291309","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/291309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=291309"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/291309\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=291309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=291309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=291309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}