我使用Rx-Android仅将1000个对象插入到Room数据库中,但至少需要7分钟。我怎样才能使其更快?
我正在使用翻新技术通过分页从服务器获取数据,并使用RxJava的zip运算符并行获取它们。我将在24秒内获得所有数据,但是当我将它们插入Room DB时,至少需要7分钟才能插入它们,有时甚至需要10分钟以上。 我试图将所有1000个对象插入在一起,分别插入100个和一个,但是每次都得到相同的结果。我已经阅读了一些有关插入延迟的文章,发现1000个对象最多需要64秒。
GroupA实体
@Entity(tableName = "group_a")
public class GroupA {
@Expose
@PrimaryKey
@NonNull
private String _id;
@Expose
@TypeConverters(AbracketseConverter.class)
private List<Abracketse> abracketses;
@Expose
private String createdate;
@Expose
private String group;
@Expose
private String headercolumna;
@Expose
private String headercolumnb;
@Expose
@TypeConverters(StringConverter.class)
private List<String> morelist;
@Expose
private Long sequence;
@Expose
private String subheader;
@Expose
private String updatedate;
}
Abracketse
public class Abracketse {
@Expose
private String columna;
@Expose
private String columnb;
@Expose
private String columnc;
@Expose
private String columnd;
@Expose
private String columne;
@Expose
private String columnf;
@Expose
private String columng;
}
房间数据库
@Database(entities = {
RecentSearchModel.class,
Calculator.class,
GroupA.class,
GroupB.class
}, version = 2, exportSchema = false)
public abstract class MyRoomDatabase extends RoomDatabase {
public abstract SearchDAO searchDAO();
public abstract CalculatorDAO calculatorDAO();
public abstract GroupADAO groupADAO();
public abstract GroupBDAO groupBDAO();
}
GroupA DAO
@Dao
public interface GroupADAO {
@Insert(onConflict = OnConflictStrategy.REPLACE)
Completable insertAllGroupA(List<GroupA> groupAList);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertGroupA(GroupA groupA);
@Query("SELECT * FROM group_a")
Single<List<GroupA>> getAllGroupA();
}
Application类中的房间实例
public static MyRoomDatabase getDatabase() {
if (DB_INSTANCE == null) {
synchronized (MyRoomDatabase.class) {
if (DB_INSTANCE == null) {
DB_INSTANCE = Room.databaseBuilder(context,
MyRoomDatabase.class, "my_database")
.fallbackToDestructiveMigration()
.build();
}
}
}
return DB_INSTANCE;
}
从服务器获取数据并将其合并
APIs apIs = RetroCliet.getClient();
List<Observable<List<GroupA>>> observableRequestList = new ArrayList<>();
for (int i = 0; i < 11; i++) {
observableRequestList.add(apIs.getGroupAMedicineList(new MedDataFetchBody(i)).subscribeOn(Schedulers.io()));
}
List<GroupA> result1 = new ArrayList<>();
Observable<List<GroupA>> result = Observable.zip(observableRequestList, objects -> {
for (Object object : objects) {
for (Object childObject : (List<GroupA>) object) {
result1.add((GroupA) childObject);
}
}
return result1;
});
result.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new Observer<List<GroupA>>() {
@Override
public void onSubscribe(Disposable d) {
disposable.add(d);
}
@Override
public void onNext(List<GroupA> groupAS) {
}
@Override
public void onError(Throwable e) {
CustomDialog.dismissDialog();
reTryDialouge();
}
@Override
public void onComplete() {
insertToDB(result1);
}
});
将数据插入会议室
Completable.fromAction(() -> MyApplication.getDatabase().groupADAO().insertAllGroupA(groupAS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new CompletableObserver() {
@Override
public void onSubscribe(Disposable d) {
disposable.add(d);
}
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
CustomDialog.dismissDialog();
}
}));
我的错在哪里?我该如何改善?如何加快插入操作?