首页 > 解决方案 > Dao 类的实例未正确初始化

问题描述

我想使用 将数据库中的数据加载到 RecyclerView 中Kotlin Coroutine,但它在第一次启动时不会加载数据,但是当 App 重新启动时它会正确加载数据。存储库类中似乎有问题:

NoteDao.kt 是:

@Dao
interface NoteDAO {

    @Query("SELECT * FROM NoteTable ORDER BY noteDate DESC")
    fun findAllNotes(): List<Note>

}

NoteRepository.kt

class NoteRepository(application: Application) {

    private var allNotes = MutableLiveData<List<Note>>()

    private val noteDAO = NoteDatabase.getDatabase(application).getNoteDao() // Issue is caused by this line because data is not assigned to noteDAO 

    fun getAllNotes(): MutableLiveData<List<Note>> {
        CoroutineScope(Dispatchers.IO).launch {
            val noteData = noteDAO.findAllNotes()
            allNotes.postValue(NoteData)
        }
        return allNotes   // it also returns empty List with size = 0
    }

}

适配器类是

class NoteAdapter(val context: Context): RecyclerView.Adapter<NoteAdapter.NoteHolder>() {

    private var allNotes: List<Note> = ArrayList()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteHolder {
        val itemView = LayoutInflater.from(parent.context)
            .inflate(R.layout.Note_item, parent, false)
        return NoteHolder(itemView)
    }

    override fun onBindViewHolder(holder: NoteHolder, position: Int) {
        val currentNote = allNotes[position]
        holder.title.text = currentNote.title
        holder.date.text = DateUtils.getDate(Date(currentNote.date))

    }

    override fun getItemCount(): Int {
        return allNotes.size
    }

    fun addNotes(allNotes: List<Note>) {
        this.allNotes = allNotes
        notifyDataSetChanged()
    }

    inner class NoteHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var title: LabelTextView = itemView.findViewById(R.id.item_title)
        var date: LabelTextView = itemView.findViewById(R.id.item_date)
    }

}

和 ViewModel 是

class NoteViewModel(
    application: Application
): AndroidViewModel(application) {

    private var repository = NoteRepository(application)

    private var _allNotes = repository.getAllNotes()

    val allNotes: LiveData<List<Note>>
        get() = _allNotes
}

最后它附加在 MainActivity

class MainActivity : AppCompatActivity(){

    private lateinit var mNoteViewModel: NoteViewModel
    private lateinit var mNoteAdapter: NoteAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)    

        mNoteAdapter = NoteAdapter(this)

        mNoteViewModel = ViewModelProviders.of(this).get(NoteViewModel::class.java)
        mNoteViewModel.allNotes.observe(
            this, Observer {
                Log.e("MAIN_ACTIVITY", "Called...") // it is printed in Log but data is not recieved from repository
                mNoteAdapter.addNotes(it)
            }
        )

        rv_log.layoutManager = GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false)

        mNoteAdapter.notifyDataSetChanged()
        rv_log.adapter = mNoteAdapter
    }

}

一切都没有加载和显示数据RecyclerView

标签: androidkotlinkotlin-coroutinesandroid-viewmodelandroid-mvvm

解决方案


似乎问题在于getAllNotes()手段CoroutineScope无法正常工作。您应该使用Flow API并将 Dao 方法更改为

@Query("SELECT * FROM NoteTable ORDER BY noteDate DESC")
fun findAllNotes(): Flow<List<Note>>

在此之后,您必须更改 NoteRepository 类并将init块设置为:

init {
    CoroutineScope(Dispatchers.IO).launch {
         val noteData = noteDAO.findAllNotes()
         noteData.collect{ // it is related to Flow API
             allNotes.postValue(it)
         }
    }
} 

allNotes然后返回getAllNotes()

fun getAllNotes(): MutableLiveData<List<Note>> {            
    return allNotes 
}

现在你必须检查它是否有效?


推荐阅读