Kotlin Singleton With Parameter

Jan 22, 2019
@Volatile private var adapter: JsonAdapter<RateAppReminder>? = nullprivate 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.

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.