I am trying to get data from Room Database using async & await inside Coroutine Scope but getting problem while returning value.
Here is my code:
fun getUserFromDB():Profile {
val profileDao = AppDatabase.getDatabase(context).getProfileDao()
CoroutineScope(Dispatchers.IO).launch {
return profileDao.getUserProfile().await()
}
}
Dao:
@Query("SELECT * FROM PROFILE LIMIT 1")
suspend fun getUserProfile():Deferred<Profile>
Here I want to return userProfile from the method but I cannot do that inside scope and it will be null if I return from outside of Coroutine scope.
Note: I am not following MVVM pattern here but doing a simple example.