Kotlin Singleton With Parameter

January 22, 2019
@Volatile private var adapter: JsonAdapter<RateAppReminder>? = null

private fun getJsonAdapter(moshi: Moshi): JsonAdapter<RateAppReminder> = adapter ?: synchronized(this) {
    moshi.adapter<RateAppReminder>(RateAppReminder::class.java)
}

NOTE: Refer to this example for optional parameter.

If no parameter is required, consider using lazy property.

val moshiStringListAdapter by lazy {
    moshi.adapter<MutableList<String>>(List::class.java)
}

NOTE: To use lazy property with parameter, do look into this.

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.