Skip to content

Fixing EnterUpcFragment so it waits for database to load before continuing

Tyler Sizse requested to merge issue_205_nest_db into master

Closes #205 (closed); EnterUpcFragment does not wait for database to be loaded/created before continuing. This may result in other classes trying to read data from the database before the database can be populated.

NOTE: This branch was merged into !210 (merged).

NOTE: Moved NestDBDataSource, NestDBOpenHelper, and NestUPC into their own package called edu.ncc.nest.nestapp.CheckExpirationDate.DatabaseClasses

NOTE: Created a new NestDBActivity class, (statically-nested inside NestDBDataSource.java), that loads the Nest.db database when the NestDBActivity is created.

NOTE: NestDBDataSource's constructor is now private so it can only be created within the NestDBActivity class. Whenever a NestDBDataSource object is needed, the underlying activity should be an instance of the new NestDBActivity class. When a NestDBDataSource object is needed within a fragment, (as long as the underlying activity is an instance of NestDBActivity), the following two methods can be used:

  • Static Method:
  NestDBDataSource dataSource = NestDBActivity.requireDataSource(this);
  • Instance Method:
  NestDBDataSource dataSource = ((NestDBActivity) requireActivity()).requireDataSource();

NOTE: When extending the NestDBActivity class the content view should be set within the onLoadSuccess method NOT the onCreate method. This prevents fragments (that may try to access the database) from being created, before the database is loaded:

  • Example:
public class DatabaseActivity extends NestDBDataSource.NestDBActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /* Don't set content view here so that fragments are NOT created while the database is
         * loading */

    }

    @Override
    protected void onLoadSuccess(@NonNull NestDBDataSource nestDBDataSource) {
        super.onLoadSuccess(nestDBDataSource);

        // If the Activity is not dead
        if (!this.isDestroyed()) {

            /* Set the content view and support action toolbar here, so that fragments are NOT created
             * until the database successfully loads. */
            setContentView(R.layout.layout_name);

        }

    }

    @Override
    protected void onLoadError(@NonNull Throwable throwable) {
        super.onLoadError(throwable);

    }

}
Edited by Tyler Sizse

Merge request reports