简体   繁体   English

我如何从 Room 数据库中获取一个项目?

[英]How can i get one item from Room database?

So, I have two ViewModels and two screens in my app.所以,我的应用程序中有两个 ViewModel 和两个屏幕。 The first screen is for the presentation of a list of diary item elements, second is for detailed information on diary items.第一个屏幕用于显示日记项目元素列表,第二个屏幕用于显示日记项目的详细信息。 In a second ViewModel, I have an id to get a record from DB but can find it.在第二个 ViewModel 中,我有一个 id 可以从 DB 获取记录,但可以找到它。 What can I do to get it?我该怎么做才能得到它?

DAO:道:

interface DiaryDao {

    @Query("SELECT * FROM diaryItems")
    fun getAllDiaryPosts(): LiveData<List<DiaryItem>>

    @Query("Select * from diaryItems where id = :id")
    fun getDiaryPostById(id: Int) : DiaryItem

    @Query("Delete from diaryItems where id = :index")
    fun deleteDiaryPostByIndex(index : Int)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertDiaryPost(diaryItem: DiaryItem)

    @Update
    suspend fun updateDiaryPost(diaryItem: DiaryItem)

    @Delete
    suspend fun deleteDiaryPost(diaryItem: DiaryItem)

    @Query("Delete from diaryItems")
    suspend fun deleteAllDiaryItems()
    
}

Repository资料库

class DiaryRepository @Inject constructor(private val diaryDao: DiaryDao) {

    val readAllData: LiveData<List<DiaryItem>> = diaryDao.getAllDiaryPosts()
   
    suspend fun getDiaryPostDyIndex(index: Int): DiaryItem {
        return diaryDao.getDiaryPostById(index)
    }
}

First viewmodel第一个视图模型

@HiltViewModel
class PostListViewModel
@Inject
constructor(
    private val diaryRepository: DiaryRepository,
) : ViewModel() {
    private var allDiaryItems: LiveData<List<DiaryItem>> = diaryRepository.readAllData
}

Second viewmodel第二视图模型

@HiltViewModel
class PostDetailViewModel
@Inject
constructor(
    private val savedStateHandle: SavedStateHandle,
    private val diaryRepository: DiaryRepository
) : ViewModel() {


    sealed class UIState {
        object Loading: UIState()
        data class Success(val currentPosts: DiaryItem) : UIState()
        object Error : UIState()
    }

    val postDetailState: State<UIState>
        get() = _postDetailState
    private val _postDetailState = mutableStateOf<UIState>(UIState.Loading)


    init {
        viewModelScope.launch (Dispatchers.IO) {
           try {

                   val diaryList: DiaryItem = diaryRepository.getDiaryPostDyIndex(2) //it is for test
                   _postDetailState.value = UIState.Success(diaryList)

           } catch (e: Exception) {
               withContext(Dispatchers.Main) {
                   _postDetailState.value = UIState.Error
               }
           }
        }

    }

}

I am sure you are getting errors.我确定您遇到了错误。 because you updating UI State in IO Thread因为您在 IO 线程中更新了 UI State

fun getDairyItem(itemId: Int){
viewModelScope.launch (Dispatchers.IO) {
     try {
           val diaryList: DiaryItem = diaryRepository.getDiaryPostDyIndex(itemId)
           withContext(Dispatchers.Main) {
              _postDetailState.value = UIState.Success(diaryList)
           } 
           } catch (e: Exception) {
               withContext(Dispatchers.Main) {
                   _postDetailState.value = UIState.Error
               }
           }
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM