我正在尝试使用 RxJava 在后台线程上使用我的 RoomDatabase。
我的 DAO 类:
import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Insert;
import java.util.List;
@Dao
public interface MeasurementDAO
{
@Insert
public void insertMeasurements(Measurements m);
}
我的实体类(为简洁起见省略了 getter 和 setter 方法):
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
import android.support.annotation.NonNull;
@Entity
public class Measurements
{
@NonNull
@PrimaryKey
public String mId;
public String finalResultIn;
public String finalResultFt;
public String lengthFt;
public String widthFt;
public String heightFt;
public String lengthIn;
public String widthIn;
public String heightIn;
public Measurements(String finalResultFt, String finalResultIn, String lengthFt, String widthFt, String heightFt,
String lengthIn, String widthIn, String heightIn)
{
this.finalResultFt = finalResultFt;
this.finalResultIn = finalResultIn;
this.lengthFt = lengthFt;
this.widthFt = widthFt;
this.heightFt = heightFt;
this.lengthIn = lengthIn;
this.widthIn = widthIn;
this.heightIn = heightIn;
}
}
最后,这是我的 MeasurementDatabase 类:
@Database(entities = {Measurements.class}, version = 1)
public abstract class MeasurementDatabase extends RoomDatabase
{
private static final String DB_NAME = "measurement_db";
private static MeasurementDatabase instance;
public static synchronized MeasurementDatabase getInstance(Context context)
{
if(instance == null)
{
instance = Room.databaseBuilder(context.getApplicationContext(), MeasurementDatabase.class,
DB_NAME)
.fallbackToDestructiveMigration()
.build();
}
return instance;
}
public abstract MeasurementDAO measurementDAO();
}
慕丝7291255
莫回无
回首忆惘然
相关分类