猿问

房间数据库 RxJava 后台线程

我正在尝试使用 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();

}


撒科打诨
浏览 101回答 3
3回答

慕丝7291255

创建一个 Observable 并在其中编写您的逻辑。您可以订阅可观察对象并获取布尔值。&nbsp;public Observable<Boolean> insertObject(Measurements m) {&nbsp; &nbsp; return Observable.create(new ObservableOnSubscribe<Boolean>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void subscribe(ObservableEmitter<Boolean> e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; appDb.measurementDAO().insertMeasurements(m);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.onNext(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.onComplete();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }).subscribeOn(Schedulers.io());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}

莫回无

我知道你有答案,但你永远不知道这是否失败。我确定您的 dao 插入方法可以返回一个long[](插入行的 ID)。你可以轻松地做到:Completable.fromCallable(() ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; appDb.measurementDAO().insertMeasurements(m).length != 0 ?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Completable.complete() :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Completable.error(new IllegalStateException("Error inserting " + m.toString())))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .subscribeOn(Schedulers.io())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .subscribe(() -> { }, Throwable::printStackTrace);

回首忆惘然

我找到了我丢失的部分:Completable.fromAction(()&nbsp;->&nbsp;appDb.measurementDAO().insertMeasurements(m)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.subscribeOn(Schedulers.io()) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.subscribe();
随时随地看视频慕课网APP

相关分类

Java
我要回答