2016-07-30 17 views
0

Mein ProgressDialog wird gestoppt, während die Methode zum Nacheinstellen der Enqueue aufgerufen wird und Daten zum Realm hinzugefügt werden. Das einzige Problem ist, progressdialog Zyklus kommt zu stoppen und es gibt keine Fehler. Hier ist mein Code.zeigt ProgressDialog an, während Daten mit Nachrüstung abgerufen und Daten zur Realm db hinzugefügt werden.

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) { 

     } 
    }); 

} 

Die Anzahl der Fragen über 400. ist Obwohl ich Handler, dass progressdialog kommt noch Radfahren stoppen und entlassen, wenn alle Daten Bereich hinzugefügt wird. Was muss ich lösen? Danke im Voraus.

Antwort

0

Sie blockieren Ihren UIhread mit den Bereichsaufrufen. Sie müssen den Code innerhalb der onResponse-Methode in einen neuen Thread verschieben.

Einfachste Lösung Realm.getDefaultInstance(). ExecuteTransactionAsync (/ * Realm-Einfügungen hier! * /);

Beste Lösung: RxJava!