Android Room 通用 DAO

Android Room Generic DAO(Android Room 通用 DAO)

本文介绍了Android Room 通用 DAO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Good day Stack,我正在开发一个使用 Android Room 1.0.0 Alpha 5 的 Android 项目,我面临的主要问题是每次我需要从 Room 调用一个 DAO 时,我需要做这样的事情:

Good day Stack, i'm working on an Android project that uses Android's Room 1.0.0 Alpha 5, the main issue that i'm facing is that every time i need to call one of the DAO from room i need to do something like this:

Activity.java:

Activity.java:

...
AppDatabase db = Room.databaseBuilder(context, AppDatabase.class, "Storage").build();
Table1 table = new Table1();
table.setId(1);
table.setName("Hello");
new AccessDB().execute(1);

/* Generic AccessDB needed */
private class AccessDB extends AsyncTask<Integer,Void,List<Table1>> {

    @Override
    protected List<Table1> doInBackground(Integer... param) {
        switch(param[0]) {
            case 1:
                return db.Table1DAO().create();
            case 2:
                return db.Table1DAO().read();
        }
        return new ArrayList<>();
    }

    @Override
    protected void onPostExecute(List<Table1> list) {
        processData(list);
    }
}
...

我知道我可以从主线程访问 Room DB,这会缩小代码,但我认为这不是一个好的做法,因为这会在每次必须处理数据时锁定活动.

I know that i can access Room DB from the main thread, and that would shrink the code, but i think that's not a good practice since that would lock the activity every time it has to handle data.

因此,如果我需要从Table2"插入或读取数据,我将不得不重新做同样的事情,如果我可以将实体类型转换为T"之类的泛型,然后再进行类似的操作,那就太好了制作一个通用的AccessDB".但由于我对 Java 不太熟悉......我目前正在为此苦苦挣扎.

So if i need to insert or read data from "Table2" i would have to do the same all over again, it would be great if i could turn the entity types into generics like "T" or something like that and then make a generic "AccessDB". But since i'm not too familiar with Java... I'm currently struggling with this.

这是实例的一些其他代码.

Here is some other code of the instances.

AppDatabase.java:

AppDatabase.java:

@Database(entities = {Table1.class, Table2.class, Table3.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
    public abstract Table1DAO Table1DAO();
    public abstract Table2DAO Table2DAO();
    public abstract Table3DAO Table3DAO();
}

Table1.java:

Table1.java:

@Entity
public class Table1 {
    /* setters & getters */
    @PrimaryKey(autoGenerate = true)
    private int id;
    private String name;
}

Table1DAO.java:

Table1DAO.java:

@Dao public interface Table1DAO {
    @Query("SELECT * FROM Table1")
    List<Table1> read(Table1 table);

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    List<Long> create(Table1... table);
}

感谢大家的帮助.

推荐答案

您可以使用继承并创建一个 BaseDao 将由您所有的子Dao 实现.这样你就不需要一遍又一遍地编写常用方法.

You can use inheritance and create a BaseDao which will be implemented by all your child Dao. This way you won't need to write the common methods again and again.

interface BaseDao<T> {

/**
 * Insert an object in the database.
 *
 * @param obj the object to be inserted.
 */
@Insert
fun insert(obj: T)

/**
 * Insert an array of objects in the database.
 *
 * @param obj the objects to be inserted.
 */
@Insert
fun insert(vararg obj: T)

/**
 * Update an object from the database.
 *
 * @param obj the object to be updated
 */
@Update
fun update(obj: T)

/**
 * Delete an object from the database
 *
 * @param obj the object to be deleted
 */
@Delete
fun delete(obj: T)
}

阅读更多相关信息:https://gist.github.com/florina-muntenescu/1c78858f286d196d545c038a71a3e864#file-basedao-kt

Florina 的原创作品.

Original credits to Florina.

这篇关于Android Room 通用 DAO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android Room 通用 DAO

基础教程推荐