我尝试通过改型来获取数据,然后使用响应主体尝试使用Work Manager将列插入到Room Database中。 但是即使doWork()方法在后台线程上工作,它仍然会在主线程异常上提供错误网络。请帮助我解决其他引入异步任务的解决方案,因为如果引入了异步任务,那么我的WorkManagers默认方法例如beginWith() ,then()将开始并行工作,而不是顺序工作。
BillTaxInfoDAO billTaxInfoDAO = db.billTaxInfoDao(); billTaxInfoDAO.insert(billTaxInfo); 这两行在主线程异常上提供了网络。
错误: 无法访问主线程上的数据库,因为它可能长时间锁定UI。 在androidx.room.RoomDatabase.assertNotMainThread(RoomDatabase.java:209) 在androidx.room.RoomDatabase.beginTransaction(RoomDatabase.java:256) 在com.witbybit.offlinepos.datamodels.pointofsale.BillTaxInfoDAO_Impl.insert(BillTaxInfoDAO_Impl.java:103) 在com.witbybit.offlinepos.restore.RestoreManager $ 4.onResponse(RestoreManager.java:196)
MyWorker类
@NonNull
@Override
public Result doWork() {
try {
Data data = getInputData();
String choice = data.getString("method");
switch (choice) {
case "back_up_found":{isBackUpfound();break;}
case "bill_tax_info": {billTaxInfoRestore();break;}
case "category": {categoryRestore();break;}
case "customer": {customerRestore();break;}
case "outlet": {outletRestore();break;}
case "payment_entry": {paymentEntryRestore();break;}
case "pos_bill": {posBillRestore();break;}
case "pos_bill_item": {posBillItemRestore();break;}
case "pos_item": {posItemRestore();break;}
case "tax": {taxRestore();break;}
case "tax_group": {taxGroupRestore();break;}
case "user": {userRestore();break;}
case "user_sms_data": {userSMSData();break;}
default: {Log.wtf(TAG, "default called");}
}
return Result.success(data);
}catch (Exception e){
Log.wtf(TAG,"Failed for "+e.getMessage() );
return Result.failure();
}
}
public void isBackUpfound(){
RestoreApiClient restoreApiClient=getRetrofit().create(RestoreApiClient.class);
Call<List<Object>> call=restoreApiClient.isBackUpFound();
call.enqueue(new Callback<List<Object>>() {
@Override
public void onResponse(Call<List<Object>> call, Response<List<Object>> response) {
for(Object object:response.body()){
HashMap<String,Double> map=new Gson().fromJson(new Gson().toJson(object),HashMap.class);
double row_count=map.get("row");
if(row_count>1){
Log.wtf("Restore Manager" ,"Backup Found");
}
}
}
@Override
public void onFailure(Call<List<Object>> call, Throwable t) {
}
});
}
private void billTaxInfoRestore(){
RestoreApiClient apiClient=RestoreManager.getRetrofit().create(RestoreApiClient.class);
Call<List<Object>> call=apiClient.getEachTableData("bill_tax_info");
call.enqueue(new Callback<List<Object>>() {
@Override
public void onResponse(Call<List<Object>> call, Response<List<Object>> response) {
for (Object object: response.body()) {
Gson gson=new Gson();
BillTaxInfo billTaxInfo=gson.fromJson(gson.toJson(object),BillTaxInfo.class);
//BillsRepository.billTaxInfoInsert(billTaxInfo,getApplicationContext());
AppDatabase db = AppDatabase.getDatabase(getApplicationContext());
BillTaxInfoDAO billTaxInfoDAO=db.billTaxInfoDao();
billTaxInfoDAO.insert(billTaxInfo);
}
Log.wtf(TAG,"bills restore complete");
}
@Override
public void onFailure(Call<List<Object>> call, Throwable t) {
Log.wtf("Restore Manager","failure "+t);
}
});
}
这是我的呼叫片段类
var tax_bill_info_request=OneTimeWorkRequest.Builder(RestoreManager::class.java!!)
.setInputData(Data.Builder().putString("method", "bill_tax_info").build())
.build()
var category_request=OneTimeWorkRequest.Builder(RestoreManager::class.java!!)
.setInputData(Data.Builder().putString("method", "category").build())
.build()
requests.add(tax_bill_info_request)
requests.add(category_request)
WorkManager.getInstance().getWorkInfoByIdLiveData(tax_bill_info_request.id).observe(this, Observer {
restore_textview.text =restore_textview.text.toString() + it.outputData.getString("method")+" "+it.state.name
})
WorkManager.getInstance().getWorkInfoByIdLiveData(category_request.id).observe(this, Observer {
restore_textview.text =restore_textview.text.toString() + it.outputData.getString("method")+" "+it.state.name
})
restoreButton?.setOnClickListener{view ->
try {
startRestore()
}catch (e:Exception){
Log.wtf(TAG,e.message)
}
}
fun startRestore(){
WorkManager.getInstance()
.beginWith(requests.get(0))
.then(requests.get(1))
.enqueue()
}
答案 0 :(得分:0)
stacktrace在WorkManager中没有任何指示。看起来您的RestoreManager#onResponse正在主线程上发生。您应该检查这段代码的状况。也许您的网络响应会自动传递到主线程。