Search code examples
androiddatabaserealm

Thread runs after statements below are executed


I am showing a countdown while Realm dataBase is loaded from the asset file

 SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(this);
            boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true);
            if (isFirstRun)
            {
                handler = new Handler();
                Runnable r = new Runnable() {
                    public void run() {
                        Intent intent=new Intent(context,FirstRun.class);
                        startActivity(intent);
                    }
                };
                handler.post(r);


                RealmConfiguration config = new RealmConfiguration.Builder(context)
                        .name(Realm.DEFAULT_REALM_NAME)
                        .migration(new in.webic.longevity.longevity.Word())
                        .assetFile(context, "Default.realm")
                        .schemaVersion(0)
                        .build();

                realm = realm.getInstance(config);
                realm.close();

                SharedPreferences.Editor editor = wmbPreference.edit();
                editor.putBoolean("FIRSTRUN", false);
                editor.commit();

            }

Problem:

countdown should be show, rather a blank screen while asset file is copying as default database

Yet activity starts after few seconds(which is in the thread) taken by the code below to load the asset file,
Is there a better way to show countdown while Realm configuration is setup.
any help would be appreciated
Thanks


Solution

  • If you want to show a countdown, you must to copy the file manually.

    And then, run the slow code in AsyncTask.

    Like this:

    public class LauncherActivity extends Activity {
    
         public void onCreate(...) {
            // init view
    
            new SlowTask().execute();
         }
    
    
         class SlowTask extends AsyncTask<Void, Void, Void>{
    
              protected Void doInBackground(Void... params) {
    
                  // realm slow code here
                  return null;
              }
    
              protected void onPostExecute(Void result) {
                 // start your next activity here
              }
         }
    }