使用 RxJava 在主线程室中执行删除
Executing delete in main thread room using RxJava
我正在使用 Room 来存储有关购物车详细信息的信息。
我想detele
在主线程中记录。我遇到错误
can not access database on the main thread since it may potentially lock the up for a long period of time
我检查了以下链接,但没有帮助我
executing delete with room (rxjava)
How to insert data and get id as out parameter in android room and rxjava 2?
DaoAccess.java
@Dao
public interface DaoAccess {
@Insert
void insertMultipleRecord(DBProducts... universities);
@Insert
void insertMultipleListRecord(List<DBProducts> universities);
@Insert
void insertOnlySingleRecord(DBProducts university);
@Query("SELECT * FROM DBProducts")
LiveData<List<DBProducts>> fetchAllData();
@Update
void updateRecord(DBProducts university);
@Delete
void deleteRecord(DBProducts university);
}
Application.class
public class PRApp extends Application {
private static PRApp m_instance;
DBHelper dbHelper;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
prefs = new PRPrefs(this);
m_instance = this;
sDefSystemLanguage = Locale.getDefault().getLanguage();
switch (sDefSystemLanguage) {
case "en":
sDefSystemLanguageCode = "1";
break;
case "ar":
sDefSystemLanguageCode = "2";
break;
default:
sDefSystemLanguageCode = "3";
break;
}
}
public DBHelper getRoomDB() {
dbHelper = Room.databaseBuilder(getApplicationContext(),
DBHelper.class, "prasukh-db").build();
return dbHelper;
}
}
我的异步任务正在运行
new AsyncTask<Void, Void, Integer>() {
@Override
protected Integer doInBackground(Void... params) {
PRApp.getInstance().getRoomDB().daoAccess().deleteRecord(products.get(position));
return 0;
}
@Override
protected void onPostExecute(Integer agentsCount) {
Utilities.decrementBadegeCount();
productsArrayList.remove(position);
cardAdapter.notifyDataSetChanged();
notifyDataSetChanged();
hideProgressDialog();
setCartLayout();
}
}.execute();
如何在 RxJava.Because 我的 DAO delete
返回 void
中实现这一点?
使用 fromCallable
运算符。
Observable.fromCallable(() -> {
PRApp.getInstance().getRoomDB().daoAccess().deleteRecord(products.get(position));
return true;
}).firstOrError()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<List<String>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onSuccess(List<String> strings) {
}
@Override
public void onError(Throwable e) {
}
});
// or only
.subscribe()
// But if an error occurred in delete operation the app will crash
这会执行删除操作并生成 Observable<Boolean>
。
或者您可以使用
允许在主线程中访问数据库
Room.databaseBuilder(getApplicationContext(),DBHelper.class, "prasukh-db")
.allowMainThreadQueries()
.build();
但由于性能原因,不鼓励这样做。
我正在使用 Room 来存储有关购物车详细信息的信息。
我想detele
在主线程中记录。我遇到错误
can not access database on the main thread since it may potentially lock the up for a long period of time
我检查了以下链接,但没有帮助我
executing delete with room (rxjava)
How to insert data and get id as out parameter in android room and rxjava 2?
DaoAccess.java
@Dao
public interface DaoAccess {
@Insert
void insertMultipleRecord(DBProducts... universities);
@Insert
void insertMultipleListRecord(List<DBProducts> universities);
@Insert
void insertOnlySingleRecord(DBProducts university);
@Query("SELECT * FROM DBProducts")
LiveData<List<DBProducts>> fetchAllData();
@Update
void updateRecord(DBProducts university);
@Delete
void deleteRecord(DBProducts university);
}
Application.class
public class PRApp extends Application {
private static PRApp m_instance;
DBHelper dbHelper;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
prefs = new PRPrefs(this);
m_instance = this;
sDefSystemLanguage = Locale.getDefault().getLanguage();
switch (sDefSystemLanguage) {
case "en":
sDefSystemLanguageCode = "1";
break;
case "ar":
sDefSystemLanguageCode = "2";
break;
default:
sDefSystemLanguageCode = "3";
break;
}
}
public DBHelper getRoomDB() {
dbHelper = Room.databaseBuilder(getApplicationContext(),
DBHelper.class, "prasukh-db").build();
return dbHelper;
}
}
我的异步任务正在运行
new AsyncTask<Void, Void, Integer>() {
@Override
protected Integer doInBackground(Void... params) {
PRApp.getInstance().getRoomDB().daoAccess().deleteRecord(products.get(position));
return 0;
}
@Override
protected void onPostExecute(Integer agentsCount) {
Utilities.decrementBadegeCount();
productsArrayList.remove(position);
cardAdapter.notifyDataSetChanged();
notifyDataSetChanged();
hideProgressDialog();
setCartLayout();
}
}.execute();
如何在 RxJava.Because 我的 DAO delete
返回 void
中实现这一点?
使用 fromCallable
运算符。
Observable.fromCallable(() -> {
PRApp.getInstance().getRoomDB().daoAccess().deleteRecord(products.get(position));
return true;
}).firstOrError()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<List<String>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onSuccess(List<String> strings) {
}
@Override
public void onError(Throwable e) {
}
});
// or only
.subscribe()
// But if an error occurred in delete operation the app will crash
这会执行删除操作并生成 Observable<Boolean>
。
或者您可以使用
允许在主线程中访问数据库Room.databaseBuilder(getApplicationContext(),DBHelper.class, "prasukh-db")
.allowMainThreadQueries()
.build();
但由于性能原因,不鼓励这样做。