Android Room通用DAO [英] Android Room Generic DAO
问题描述
Good day Stack,我正在一个使用Android Room 1.0.0 Alpha 5的Android项目上工作,我面临的主要问题是每次我需要从房间中调用一个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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!