發表文章

Kotlin (5): Coroutines

圖片
學習Kotlin最重要的項目,就是Coroutines. 這可是之前Java所沒有的,Java的Thread 不好用,這個coroutines才是適合非同步領域的東西. Coroutines 這個單字是由兩個英文單字合併而成的,分別是 cooperation + routine.用來做非同步和非阻塞任務.意思就是要各個子任務程協作運行的意思,所以明白了它被創造出來是要解決異步問題的。 Kotlin的協程完美復刻了Go語言的協程設計模式( 作用域/channel/select), 將作用域用對象分類; 可以更好地控制作用域生命週期; await模式(類似JavaScript的異步任務解決方案) Kotlin參考RxJava響應式框架創造出Flow 使用協程開始就不需要考慮線程的問題, 只需要在不同場景使用不同的調度器就好 github:  https://github.com/Kotlin/kotlinx.coroutines kotlin home:  https://kotlinlang.org reference:  https://juejin.cn/post/6987724340775108622 仔細看完文件後,發現coroutines並不是那麼簡單,反而使用方法有點複雜. ---- dependencies { implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:+' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:+' } ----- coroutine 典型用法, 啟動協程: 啟動一個新的協程, 常用的主要有以下幾種方式:它們被稱為coroutine builders.  launch async (可從Coroutine 返回值) runBlocking ---- launch– Launches new coroutine without blocking current thread and returns a reference to the coroutine as a Job. The coroutine is canceled when the resu

Retrofit in Android

圖片
現在Retrofit 超重要的,以前都使用原始的okhttp 自己轉換json. 但業界作法是用Retrofit, 所以這技能是一定要會的,所以今天我就親自來實作看看. 什麼是Retrofit ? Retrofit is type-safe REST client for Android and Java which aims to make it easier to consume RESTful web services.  It makes it relatively easy to retrieve and upload JSON via a REST based webservice.  In Retrofit you configure which converter is used for the data serialization.   Typically for JSON you use GSon, but you can add custom converters to process XML or other protocols.  Retrofit uses the OkHttp library for HTTP requests ---- Retrofit vs  Volley? Retrofit is a purely abstract layer above okhttp used for network calls and it takes away the boiler plate required for writing those lengthy AsynTasks. So, Retrofit is just another library like Volley, EasyNetwork but it has support for integrating with RxAndroid as well as live data classes from Android Architecture. Retrofit is not related to Java programming.. It is related to Android concepts .. Retrofit is a REST Client for Jav

Kotlin (4): Getters and Setters

Properties in Kotlin var author: String = "Steven" This is the same as defining the following get() and set() methods: (example 1:) var author: String = " Steven "     get() {         return field     }     set(value) {         field = value     } --- (example 2:) class Rectangle(val width: Int, val height: Int) {     val area: Int // property type is optional since it can be inferred from the getter's return type         get() = this.width * this.height } --- (example 3:) var stringRepresentation: String     get() = this.toString()     set(value) {         setDataFromString(value)      } ---- Backing fields: Every property we define is backed by a field that can only be accessed within its   get()   and   set()   methods using   the special  field  keyword . (example:) var counter = 0      set(value) {         if (value >= 0) field = value     } or var rating: Int = 5 get () { if (field < 5 ) { print( "Warning: this is a

Kotlin (3) : when

圖片
今早去看了一下折疊式手機, 還真有點厚,但展開來之後可以取代平板. 價格有點貴,因為螢幕多了一片在摺起來的外殼,這樣也不能裝保護套,  ---------------------- Kotlin 的When 非常強, 等同取代了 java中的Switch. 最基本的用法描述如下: when ( x ) { 1 -> print ( "x == 1" ) 2 -> print ( "x == 2" ) else -> { print ( "x is neither 1 nor 2" ) } } ---- 1 when(view.visibility){ 2 View.VISIBLE -> toast("visible" ) 3 View.INVISIBLE -> toast("invisible" ) 4 else -> toast("gone" ) 5 } 然後他可以用判斷多種結果, 代替重複的部分. when ( x ) { 0 , 1 -> print ( "x == 0 or x == 1" ) else -> print ( "otherwise" ) } --- 1 val res = when { 2 x in 1..10 -> "cheap" 3 s.contains("hello") -> "it's a welcome!" 4 v is ViewGroup -> "child count: ${v.getChildCount()}" 5 else -> "" 6 } --- 1 override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId)