18

[Kotlin] Lazy Loading – Android

 3 years ago
source link: http://chintanrathod.com/kotlin-lazy-loading-android/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

[Kotlin] Lazy Loading – Android

You can find several benefits of using lazy loading in your application. Lazy loading will result in faster startup due to loading is deferred when variable is accessed. This is very useful when using Kotlin for Android app over web app. For mobile applications, we require to reduce application startup time so that the user feels the app content faster, rather looking at loading screen for several seconds.

It is more memory efficient, as we only load those resources which are necessary to start the application. This is very crucial while developing Android application because it is working on shared memory and resources. For instance, if you are developing a social application, user only check for chats rather profile and other friends requests. So this could be lazy loaded when user wanted.

Check below code, as soon as first reference to Retrofit is made, the Retrofit library will be initialized.

val friendsApi: FriendsgApi by lazy {
    val retrofit: Retrofit = Retrofit.Builder()
            .baseUrl(API_URL)
            .addConverterFactory(MoshiConverterFactory.create())
            .build()
    retrofit.create(FriendsgApi::class.java)
Views: 18
000000

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK