ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Mastering Android ViewModels: Essential Dos and Don’ts Part 5 🛠️5️⃣

Reza
ProAndroidDev
Published in
4 min readOct 26, 2024

We’ve Covered So Far 🔄🔄🔄

In this article we’ll cover:

#8 — Avoid Hardcoding Coroutine Dispatchers

The Problem with Hardcoding Dispatchers

Recommended Approach

class MyViewModel(
private val ioDispatcher: CoroutineDispatcher = Dispatchers.IO
) : ViewModel() {

private fun loadData() {
viewModelScope.launch(ioDispatcher) {
// Your coroutine code here
}
}
}

#9 — Unit Test Your ViewModels

Testing Challenges

Best Practices for Testing ViewModels

#10 — Avoid Exposing Suspended Functions

Why It’s Problematic

The Better Way

Conclusion:

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Responses (1)

Write a response

Great Article, just cloned the dictonary app to see these best practices in action.

--