房间数据库 RxJava 后台线程
Room Database RxJava Background Thread
我正在尝试使用 RxJava 在后台线程上使用我的 RoomDatabase。
我的 DAO class:
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);
}
我的实体 class(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 class:
@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();
}
在我的片段中,我试图在单击菜单项后插入后台线程:
最终测量数据库 appDb =
MeasurementDatabase.getInstance(getActivity());
//fill the values with the appropriate;
final Measurements m = new Measurements(
cubicInches.getText().toString(),
cubicFeet.getText().toString(),
len_ft.getText().toString(),
width_ft.getText().toString(),
height_ft.getText().toString(),
len_in.getText().toString(),
width_in.getText().toString(),
height_in.getText().toString());
Observable.just(appDb)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<MeasurementDatabase>(){
@Override
public void onSubscribe(Disposable d) {
appDb.measurementDAO().insertMeasurements(m);
}
@Override
public void onNext(MeasurementDatabase measurementDatabase)
{
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
我收到一条错误消息:
java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
我的 RxJava 代码中缺少什么没有将进程放在后台线程上?
感谢@CommonsWare 提供的一些线索,我找到了我丢失的部分:
Completable.fromAction(() -> appDb.measurementDAO().insertMeasurements(m))
.subscribeOn(Schedulers.io())
.subscribe();
创建一个 Observable 并在其中编写您的逻辑。您可以订阅可观察对象并获取布尔值。
public Observable<Boolean> insertObject(Measurements m) {
return Observable.create(new ObservableOnSubscribe<Boolean>() {
@Override
public void subscribe(ObservableEmitter<Boolean> e) {
appDb.measurementDAO().insertMeasurements(m);
e.onNext(true);
e.onComplete();
}
}).subscribeOn(Schedulers.io());
}
我知道你有答案,但你永远不知道这是否会失败。我确定您的 dao 插入方法可以 return a long[]
(插入行的 ID)。
你可以轻松做到:
Completable.fromCallable(() ->
appDb.measurementDAO().insertMeasurements(m).length != 0 ?
Completable.complete() :
Completable.error(new IllegalStateException("Error inserting " + m.toString())))
.subscribeOn(Schedulers.io())
.subscribe(() -> { }, Throwable::printStackTrace);
我正在尝试使用 RxJava 在后台线程上使用我的 RoomDatabase。
我的 DAO class:
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);
}
我的实体 class(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 class:
@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();
}
在我的片段中,我试图在单击菜单项后插入后台线程:
最终测量数据库 appDb =
MeasurementDatabase.getInstance(getActivity());
//fill the values with the appropriate;
final Measurements m = new Measurements(
cubicInches.getText().toString(),
cubicFeet.getText().toString(),
len_ft.getText().toString(),
width_ft.getText().toString(),
height_ft.getText().toString(),
len_in.getText().toString(),
width_in.getText().toString(),
height_in.getText().toString());
Observable.just(appDb)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<MeasurementDatabase>(){
@Override
public void onSubscribe(Disposable d) {
appDb.measurementDAO().insertMeasurements(m);
}
@Override
public void onNext(MeasurementDatabase measurementDatabase)
{
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
我收到一条错误消息:
java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
我的 RxJava 代码中缺少什么没有将进程放在后台线程上?
感谢@CommonsWare 提供的一些线索,我找到了我丢失的部分:
Completable.fromAction(() -> appDb.measurementDAO().insertMeasurements(m))
.subscribeOn(Schedulers.io())
.subscribe();
创建一个 Observable 并在其中编写您的逻辑。您可以订阅可观察对象并获取布尔值。
public Observable<Boolean> insertObject(Measurements m) {
return Observable.create(new ObservableOnSubscribe<Boolean>() {
@Override
public void subscribe(ObservableEmitter<Boolean> e) {
appDb.measurementDAO().insertMeasurements(m);
e.onNext(true);
e.onComplete();
}
}).subscribeOn(Schedulers.io());
}
我知道你有答案,但你永远不知道这是否会失败。我确定您的 dao 插入方法可以 return a long[]
(插入行的 ID)。
你可以轻松做到:
Completable.fromCallable(() ->
appDb.measurementDAO().insertMeasurements(m).length != 0 ?
Completable.complete() :
Completable.error(new IllegalStateException("Error inserting " + m.toString())))
.subscribeOn(Schedulers.io())
.subscribe(() -> { }, Throwable::printStackTrace);