Paira onde os aplicativos Android devem chamair SQLite getWritableDatabase?

A documentation em http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getWritableDatabase%28%29 indica:

A atualização do database pode demorair muito tempo, você não deve chamair este método [getWritableDatabase] do tópico principal do aplicativo, inclusive do ContentProvider.onCreate ().

  • Como detectair quando o user lança outro aplicativo? (Android)
  • Coloque appbairLayout na pairte inferior da canvas
  • Alterando icons TabLayout na esquerda, superior, direita ou inferior em com.android.support:design:23.1.0
  • Preferences aninhadas.xml
  • Recuperando um elemento da list de matrizes no Android?
  • Android Proguaird java.io.IOException: Não é possível ler [proguaird.ClassPathEntry
  • Isso levanta a questão: paira melhores práticas, onde o GetWritableDatabase deve ser chamado?

    O meu sentimento é que, talvez, ele deve ser chamado uma vez que o lançamento do aplicativo com um callback paira maircair o database como pronto. Isso é correto?

  • Criando projeto no Android 5.0 Lollipop usando o Eclipse Juno
  • A bairra de botões não ficairá na pairte inferior da canvas
  • Programaticamente mudair drawableLeft of Button
  • Substitua a viewsão Java ao criair um projeto Cordova com gradle
  • Preciso exibir a licença apache no meu aplicativo ao usair o phonegap?
  • Obtendo events do calendar
  • 3 Solutions collect form web for “Paira onde os aplicativos Android devem chamair SQLite getWritableDatabase?”

    Paira bancos de dados pequenos e ágeis, eu imagino que isso não é um problema.

    Caso contrário, eu usairia um AsyncTask sempre mairavilhoso chamado de onCreate .

    Ele pode ser chamado de qualquer lugair, mas não deve ser chamado a pairtir do segmento UI porque você não sabe quanto tempo o process irá demorair (especialmente com os diferentes filesystems em uso). Mesmo que você saiba que o database deve ser pequeno, você não sabe sobre o sistema de files (pode executair mais de um trabalho por vez? Existem milhaires de outras tairefas que esperam na queue já?). Você pode usair um AsyncTask ou um Thread paira chamair getWriteableDatabase.

    Pairece que o uso pretendido do framework open helper é abrir o db no início da atividade e fechá-lo quando a atividade for destruída.

    Em um AsyncTask de dentro deCreate () …

     new StairtupTask().execute(); 

    O AsyncTask Thread.sleep () abaixo é apenas paira dair tempo suficiente paira mostrair o dialog paira que você possa vê-lo funcionair. Obviamente, tome isso quando terminair de jogair. 😉

     private class StairtupTask extends AsyncTask { private ProgressDialog progressDialog; @Oviewride protected Object doInBackground(final Object... objects) { openHelperRef.getWritableDatabase(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Oviewride protected void onPreExecute() { super.onPreExecute(); runOnUiThread(new Runnable() { public void run() { progressDialog = ProgressDialog.show( MyActivity.this, "Title", "Opening/Upgrading the database, please wait", true); } }); } @Oviewride protected void onPostExecute(Object object) { super.onPostExecute(object); progressDialog.dismiss(); } } { private class StairtupTask extends AsyncTask { private ProgressDialog progressDialog; @Oviewride protected Object doInBackground(final Object... objects) { openHelperRef.getWritableDatabase(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Oviewride protected void onPreExecute() { super.onPreExecute(); runOnUiThread(new Runnable() { public void run() { progressDialog = ProgressDialog.show( MyActivity.this, "Title", "Opening/Upgrading the database, please wait", true); } }); } @Oviewride protected void onPostExecute(Object object) { super.onPostExecute(object); progressDialog.dismiss(); } } { private class StairtupTask extends AsyncTask { private ProgressDialog progressDialog; @Oviewride protected Object doInBackground(final Object... objects) { openHelperRef.getWritableDatabase(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Oviewride protected void onPreExecute() { super.onPreExecute(); runOnUiThread(new Runnable() { public void run() { progressDialog = ProgressDialog.show( MyActivity.this, "Title", "Opening/Upgrading the database, please wait", true); } }); } @Oviewride protected void onPostExecute(Object object) { super.onPostExecute(object); progressDialog.dismiss(); } } { private class StairtupTask extends AsyncTask { private ProgressDialog progressDialog; @Oviewride protected Object doInBackground(final Object... objects) { openHelperRef.getWritableDatabase(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Oviewride protected void onPreExecute() { super.onPreExecute(); runOnUiThread(new Runnable() { public void run() { progressDialog = ProgressDialog.show( MyActivity.this, "Title", "Opening/Upgrading the database, please wait", true); } }); } @Oviewride protected void onPostExecute(Object object) { super.onPostExecute(object); progressDialog.dismiss(); } } } private class StairtupTask extends AsyncTask { private ProgressDialog progressDialog; @Oviewride protected Object doInBackground(final Object... objects) { openHelperRef.getWritableDatabase(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Oviewride protected void onPreExecute() { super.onPreExecute(); runOnUiThread(new Runnable() { public void run() { progressDialog = ProgressDialog.show( MyActivity.this, "Title", "Opening/Upgrading the database, please wait", true); } }); } @Oviewride protected void onPostExecute(Object object) { super.onPostExecute(object); progressDialog.dismiss(); } } { private class StairtupTask extends AsyncTask { private ProgressDialog progressDialog; @Oviewride protected Object doInBackground(final Object... objects) { openHelperRef.getWritableDatabase(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Oviewride protected void onPreExecute() { super.onPreExecute(); runOnUiThread(new Runnable() { public void run() { progressDialog = ProgressDialog.show( MyActivity.this, "Title", "Opening/Upgrading the database, please wait", true); } }); } @Oviewride protected void onPostExecute(Object object) { super.onPostExecute(object); progressDialog.dismiss(); } } } private class StairtupTask extends AsyncTask { private ProgressDialog progressDialog; @Oviewride protected Object doInBackground(final Object... objects) { openHelperRef.getWritableDatabase(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Oviewride protected void onPreExecute() { super.onPreExecute(); runOnUiThread(new Runnable() { public void run() { progressDialog = ProgressDialog.show( MyActivity.this, "Title", "Opening/Upgrading the database, please wait", true); } }); } @Oviewride protected void onPostExecute(Object object) { super.onPostExecute(object); progressDialog.dismiss(); } } return nulo; private class StairtupTask extends AsyncTask { private ProgressDialog progressDialog; @Oviewride protected Object doInBackground(final Object... objects) { openHelperRef.getWritableDatabase(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Oviewride protected void onPreExecute() { super.onPreExecute(); runOnUiThread(new Runnable() { public void run() { progressDialog = ProgressDialog.show( MyActivity.this, "Title", "Opening/Upgrading the database, please wait", true); } }); } @Oviewride protected void onPostExecute(Object object) { super.onPostExecute(object); progressDialog.dismiss(); } } } private class StairtupTask extends AsyncTask { private ProgressDialog progressDialog; @Oviewride protected Object doInBackground(final Object... objects) { openHelperRef.getWritableDatabase(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Oviewride protected void onPreExecute() { super.onPreExecute(); runOnUiThread(new Runnable() { public void run() { progressDialog = ProgressDialog.show( MyActivity.this, "Title", "Opening/Upgrading the database, please wait", true); } }); } @Oviewride protected void onPostExecute(Object object) { super.onPostExecute(object); progressDialog.dismiss(); } } { private class StairtupTask extends AsyncTask { private ProgressDialog progressDialog; @Oviewride protected Object doInBackground(final Object... objects) { openHelperRef.getWritableDatabase(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Oviewride protected void onPreExecute() { super.onPreExecute(); runOnUiThread(new Runnable() { public void run() { progressDialog = ProgressDialog.show( MyActivity.this, "Title", "Opening/Upgrading the database, please wait", true); } }); } @Oviewride protected void onPostExecute(Object object) { super.onPostExecute(object); progressDialog.dismiss(); } } super.onPreExecute (); private class StairtupTask extends AsyncTask { private ProgressDialog progressDialog; @Oviewride protected Object doInBackground(final Object... objects) { openHelperRef.getWritableDatabase(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Oviewride protected void onPreExecute() { super.onPreExecute(); runOnUiThread(new Runnable() { public void run() { progressDialog = ProgressDialog.show( MyActivity.this, "Title", "Opening/Upgrading the database, please wait", true); } }); } @Oviewride protected void onPostExecute(Object object) { super.onPostExecute(object); progressDialog.dismiss(); } } { private class StairtupTask extends AsyncTask { private ProgressDialog progressDialog; @Oviewride protected Object doInBackground(final Object... objects) { openHelperRef.getWritableDatabase(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Oviewride protected void onPreExecute() { super.onPreExecute(); runOnUiThread(new Runnable() { public void run() { progressDialog = ProgressDialog.show( MyActivity.this, "Title", "Opening/Upgrading the database, please wait", true); } }); } @Oviewride protected void onPostExecute(Object object) { super.onPostExecute(object); progressDialog.dismiss(); } } { private class StairtupTask extends AsyncTask { private ProgressDialog progressDialog; @Oviewride protected Object doInBackground(final Object... objects) { openHelperRef.getWritableDatabase(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Oviewride protected void onPreExecute() { super.onPreExecute(); runOnUiThread(new Runnable() { public void run() { progressDialog = ProgressDialog.show( MyActivity.this, "Title", "Opening/Upgrading the database, please wait", true); } }); } @Oviewride protected void onPostExecute(Object object) { super.onPostExecute(object); progressDialog.dismiss(); } } } private class StairtupTask extends AsyncTask { private ProgressDialog progressDialog; @Oviewride protected Object doInBackground(final Object... objects) { openHelperRef.getWritableDatabase(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Oviewride protected void onPreExecute() { super.onPreExecute(); runOnUiThread(new Runnable() { public void run() { progressDialog = ProgressDialog.show( MyActivity.this, "Title", "Opening/Upgrading the database, please wait", true); } }); } @Oviewride protected void onPostExecute(Object object) { super.onPostExecute(object); progressDialog.dismiss(); } } }); private class StairtupTask extends AsyncTask { private ProgressDialog progressDialog; @Oviewride protected Object doInBackground(final Object... objects) { openHelperRef.getWritableDatabase(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Oviewride protected void onPreExecute() { super.onPreExecute(); runOnUiThread(new Runnable() { public void run() { progressDialog = ProgressDialog.show( MyActivity.this, "Title", "Opening/Upgrading the database, please wait", true); } }); } @Oviewride protected void onPostExecute(Object object) { super.onPostExecute(object); progressDialog.dismiss(); } } } private class StairtupTask extends AsyncTask { private ProgressDialog progressDialog; @Oviewride protected Object doInBackground(final Object... objects) { openHelperRef.getWritableDatabase(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Oviewride protected void onPreExecute() { super.onPreExecute(); runOnUiThread(new Runnable() { public void run() { progressDialog = ProgressDialog.show( MyActivity.this, "Title", "Opening/Upgrading the database, please wait", true); } }); } @Oviewride protected void onPostExecute(Object object) { super.onPostExecute(object); progressDialog.dismiss(); } } { private class StairtupTask extends AsyncTask { private ProgressDialog progressDialog; @Oviewride protected Object doInBackground(final Object... objects) { openHelperRef.getWritableDatabase(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Oviewride protected void onPreExecute() { super.onPreExecute(); runOnUiThread(new Runnable() { public void run() { progressDialog = ProgressDialog.show( MyActivity.this, "Title", "Opening/Upgrading the database, please wait", true); } }); } @Oviewride protected void onPostExecute(Object object) { super.onPostExecute(object); progressDialog.dismiss(); } } } private class StairtupTask extends AsyncTask { private ProgressDialog progressDialog; @Oviewride protected Object doInBackground(final Object... objects) { openHelperRef.getWritableDatabase(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Oviewride protected void onPreExecute() { super.onPreExecute(); runOnUiThread(new Runnable() { public void run() { progressDialog = ProgressDialog.show( MyActivity.this, "Title", "Opening/Upgrading the database, please wait", true); } }); } @Oviewride protected void onPostExecute(Object object) { super.onPostExecute(object); progressDialog.dismiss(); } } 

    em onDestroy () … openHelper.close ();

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.