简体   繁体   English

不确定如何使用 DAO 将 Cursor 转换为此方法的返回类型

[英]Not sure how to convert a Cursor to this method's return type with DAO

I have an issue with return type in DAO interface.我对 DAO 接口中的返回类型有疑问。 I have made research and I have tried make it without/with suspend and still the same.我已经进行了研究,并且尝试在不挂起/挂起的情况下进行,但仍然相同。

Error: Not sure how to convert a Cursor to this method's return type ...

Dao

    @Query("SELECT * FROM notification_list ORDER BY id ASC")
    abstract fun readAllDataState(): DataState<List<NotificationItemsResponse.NotificationItemData>>

My UseCase:我的用例:

class GetNotificationListItemDetailsUseCase @Inject constructor(private val notificationDao: NotificationDao): BaseFlowUseCase<Unit, DataState<List<NotificationItemsResponse.NotificationItemData>>>() {
    override fun create(params: Unit): Flow<DataState<List<NotificationItemsResponse.NotificationItemData>>> {
        return flow{
            emit(DataState.Loading)

            try {
                emit(DataState.Success(notificationDao.readAllDataState()))
            } catch(e: Exception) {
                emit(DataState.Error(e)) // error, and send the exception
            }

ViewModel视图模型

    val notificationData = MutableStateFlow<List<NotificationItemsResponse.NotificationItemData>>(emptyList())

[..]
    fun getActualState() {
        viewModelScope.launch {
            getNotificationListItemDetailsUseCase.build(Unit).collect {
                notificationData.value = it
                Log.d("test2", "${notificationData.value}")

            }
        }
    }

Fragment分段

        GlobalScope.launch { collectNotificationItems() }

[..]

    private suspend fun collectNotificationItems() {
        vm.notificationData.collectLatest { dataState ->
            when(dataState) {
                is DataState.Error -> {
                    collectErrorState()
                    Log.d("collectNotificationItems", "Collect ErrorState")
                }
                DataState.Loading -> {
                    Log.d("collectNotificationItems", "Collect Loading")
                }
                is DataState.Success<*> -> {
                    vm.notificationData.collectWith(viewLifecycleOwner) {
                        notificationAdapter.items = it
                        notificationAdapter.notifyDataSetChanged()
                        Log.d("collectNotificationItems", "Collect Sucess")
                    }
                }
            }
        }

There are all details above I hope it is enough to solve this issue.上面有所有细节我希望它足以解决这个问题。

Room doesn't know how to return a DataState type. Room 不知道如何返回 DataState 类型。 Change readAllDataState() 's return type to List<NotificationItemsResponse.NotificationItemData> and make the function suspend , that way, you'll get only one value for each call. readAllDataState()的返回类型更改为List<NotificationItemsResponse.NotificationItemData>并使 function suspend ,这样,每次调用只会获得一个值。 Then, just wrap the returned value in emit(DataState.Success(returnedValue)) .然后,只需将返回值包装在emit(DataState.Success(returnedValue))中。 Like this:像这样:

@Query("SELECT * FROM notification_list ORDER BY id ASC")
abstract suspend fun readAllDataState(): List<NotificationItemsResponse.NotificationItemData>
class GetNotificationListItemDetailsUseCase @Inject constructor(private val notificationDao: NotificationDao): BaseFlowUseCase<Unit, DataState<List<NotificationItemsResponse.NotificationItemData>>>() {

  override fun create(params: Unit): Flow<DataState<List<NotificationItemsResponse.NotificationItemData>>> = 
    flow {
        emit(DataState.Loading)
        try {
          emit(DataState.Success(notificationDao.readAllDataState()))
        } catch(e: Exception) {
          emit(DataState.Error(e)) // error, and send the exception
        }
    }

暂无
暂无

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

相关问题 Paging3:在 Room DAO 中使用 PagingSource 作为返回类型时,“不确定如何将 Cursor 转换为此方法的返回类型” - Paging3: “Not sure how to convert a Cursor to this method's return type” when using PagingSource as return type in Room DAO 不确定如何在 Room Android 中将 Cursor 转换为该方法的返回类型? - Not sure how to convert a Cursor to this method's return type in Room Android? 查询错误:不确定如何将 Cursor 转换为此方法的返回类型 - Query error: Not sure how to convert a Cursor to this method's return type 协程流程:不确定如何将光标转换为该方法的返回类型 - coroutine Flow : Not sure how to convert a Cursor to this method's return type 不确定如何将游标转换为此方法的返回类型 - Not sure how to convert a Cursor to this method's return type 房间“不确定如何将光标转换为此方法的返回类型” - Room "Not sure how to convert a Cursor to this method's return type" 房间“不确定如何将 Cursor 转换为此方法的返回类型”:哪种方法? - Room "Not sure how to convert a Cursor to this method's return type": which method? 如何解决“不确定如何将游标转换为此方法的返回类型”在ROOM Android中 - How to fix 'Not sure how to convert a Cursor to this method's return type' In ROOM android 房间:错误:不确定如何将 Cursor 转换为该方法的返回类型 (void) - Room: error: Not sure how to convert a Cursor to this method's return type (void) Android 房间不确定如何将 cursor 转换为方法的返回类型问题 - Android Room not sure how to convert cursor to method's return type issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM