Android Room Database,不推荐使用Asynctask这种方法可以用于插入吗?这是有效的方法吗?

问题描述

公共类WordRepository {

private Handler mainHandler;
private WordDao mwordDao;
private LiveData<List<Word>> mAllWords;

public WordRepository(Application application) {
    WordRoomDatabase db = WordRoomDatabase.getDatabase(application);
    mwordDao = db.wordDao();

    //get all the words and put it here into the live list

    mAllWords = mwordDao.getAllWords();
}

LiveData<List<Word>> getAllWords(){
    return mAllWords;
}

//使用可运行接口将单词插入数据库

public void insert(Word word){

//使用我创建的可运行接口插入单词

   InsertRunnable insertRunnable = new InsertRunnable(mwordDao,word);
    new Thread(insertRunnable).start();
}

//实现可运行的界面

private class InsertRunnable implements Runnable {

    private WordDao mWordDao;
    private Word word;

    //we will pass the wordDao from the outer class to this classes 
     constructor and the word that needs to inserted

    public InsertRunnable(WordDao mWordDao,Word word) {
        this.mWordDao = mWordDao;
        this.word = word;
    }
    @Override
    public void run() {
        mWordDao.insert(word);
    }
}

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)