在通过改造获取数据并将数据添加到领域数据库时显示 ProgressDialog
showing ProgressDialog while getting data with retrofit and adding data to realm db
我的 ProgressDialog 在调用 retrofit enqueue 方法并将数据添加到领域时停止。唯一的问题是 progressdialog 循环停止并且没有错误。这是我的代码。
private void DownloadQuestions(final String chapterId){
final Call<Questions> questionCall = MainApi.createService(MainService.class).
getQuestionsByChapterId(chapterId);
Log.d("Package_Name", getApplicationContext().getPackageName());
pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE);
pDialog.getProgressHelper().setBarColor(Color.parseColor("#272E3E"));
pDialog.setTitleText("Downloading");
pDialog.show();
questionCall.enqueue(new Callback<Questions>() {
@Override
public void onResponse(Call<Questions> call, Response<Questions> response) {
questions.addAll(response.body().getQuestions());
for (Question ques : questions) {
realm.beginTransaction();
QuestionRealm questionRealm = realm.createObject(QuestionRealm.class);
questionRealm.setChapter_id(ques.getChapter_id());
questionRealm.setQuestion_type_id(ques.getQuestion_type_id());
questionRealm.setQuestion(ques.getQuestion());
questionRealm.setHint1(ques.getHint1());
questionRealm.setHint2(ques.getHint2());
questionRealm.setHint3(ques.getHint3());
questionRealm.setHint4(ques.getHint4());
questionRealm.setAnswer(ques.getAnswer());
questionRealm.setYear(ques.getYear());
realm.commitTransaction();
}
pDialog.dismissWithAnimation();
Toast.makeText(getApplicationContext(), " Questions downloaded", Toast.LENGTH_SHORT).show();
quesDownload.setVisibility(View.GONE);
chapCheckBox.setVisibility(View.VISIBLE);
}
@Override
public void onFailure(Call<Questions> call, Throwable t) {
}
});
}
问题数量超过 400。虽然我使用处理程序,但当所有数据都添加到领域时,进度对话框仍然停止循环并关闭。我需要解决什么?提前致谢。
您正在用领域调用阻塞您的 UI 线程。您需要将 onResponse 方法中的代码移动到新线程。
最简单的解决方案 Realm.getDefaultInstance().executeTransactionAsync(/* 此处插入领域!*/);
最佳解决方案:RxJava!
我的 ProgressDialog 在调用 retrofit enqueue 方法并将数据添加到领域时停止。唯一的问题是 progressdialog 循环停止并且没有错误。这是我的代码。
private void DownloadQuestions(final String chapterId){
final Call<Questions> questionCall = MainApi.createService(MainService.class).
getQuestionsByChapterId(chapterId);
Log.d("Package_Name", getApplicationContext().getPackageName());
pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE);
pDialog.getProgressHelper().setBarColor(Color.parseColor("#272E3E"));
pDialog.setTitleText("Downloading");
pDialog.show();
questionCall.enqueue(new Callback<Questions>() {
@Override
public void onResponse(Call<Questions> call, Response<Questions> response) {
questions.addAll(response.body().getQuestions());
for (Question ques : questions) {
realm.beginTransaction();
QuestionRealm questionRealm = realm.createObject(QuestionRealm.class);
questionRealm.setChapter_id(ques.getChapter_id());
questionRealm.setQuestion_type_id(ques.getQuestion_type_id());
questionRealm.setQuestion(ques.getQuestion());
questionRealm.setHint1(ques.getHint1());
questionRealm.setHint2(ques.getHint2());
questionRealm.setHint3(ques.getHint3());
questionRealm.setHint4(ques.getHint4());
questionRealm.setAnswer(ques.getAnswer());
questionRealm.setYear(ques.getYear());
realm.commitTransaction();
}
pDialog.dismissWithAnimation();
Toast.makeText(getApplicationContext(), " Questions downloaded", Toast.LENGTH_SHORT).show();
quesDownload.setVisibility(View.GONE);
chapCheckBox.setVisibility(View.VISIBLE);
}
@Override
public void onFailure(Call<Questions> call, Throwable t) {
}
});
}
问题数量超过 400。虽然我使用处理程序,但当所有数据都添加到领域时,进度对话框仍然停止循环并关闭。我需要解决什么?提前致谢。
您正在用领域调用阻塞您的 UI 线程。您需要将 onResponse 方法中的代码移动到新线程。
最简单的解决方案 Realm.getDefaultInstance().executeTransactionAsync(/* 此处插入领域!*/);
最佳解决方案:RxJava!