Store constructor

Store(
  1. ModelDefinition defs,
  2. {String directory,
  3. int maxDBSizeInKB,
  4. int fileMode,
  5. int maxReaders}
)

Creates a BoxStore using the model definition from your objectbox.g.dart file.

For example in a Dart app:

var store = Store(getObjectBoxModel());

Or for a Flutter app:

getApplicationDocumentsDirectory().then((dir) {
  _store = Store(getObjectBoxModel(), directory: dir.path + "/objectbox");
});

See our examples for more details.

Implementation

Store(this.defs,
    {String directory, int maxDBSizeInKB, int fileMode, int maxReaders}) {
  var model = Model(defs.model);

  var opt = bindings.obx_opt();
  checkObxPtr(opt, 'failed to create store options');

  try {
    checkObx(bindings.obx_opt_model(opt, model.ptr));
    if (directory != null && directory.isNotEmpty) {
      var cStr = Utf8.toUtf8(directory);
      try {
        checkObx(bindings.obx_opt_directory(opt, cStr));
      } finally {
        free(cStr);
      }
    }
    if (maxDBSizeInKB != null && maxDBSizeInKB > 0) {
      bindings.obx_opt_max_db_size_in_kb(opt, maxDBSizeInKB);
    }
    if (fileMode != null && fileMode >= 0) {
      bindings.obx_opt_file_mode(opt, fileMode);
    }
    if (maxReaders != null && maxReaders > 0) {
      bindings.obx_opt_max_readers(opt, maxReaders);
    }
  } catch (e) {
    bindings.obx_opt_free(opt);
    rethrow;
  }
  _cStore = bindings.obx_store_open(opt);

  try {
    checkObxPtr(_cStore, 'failed to create store');
  } on ObjectBoxException catch (e) {
    // Recognize common problems when trying to open/create a database
    // 10199 = OBX_ERROR_STORAGE_GENERAL
    if (e.nativeCode == 10199 &&
        e.nativeMsg != null &&
        e.nativeMsg.contains('Dir does not exist')) {
      // 13 = permissions denied, 30 = read-only filesystem
      if (e.nativeMsg.endsWith(' (13)') || e.nativeMsg.endsWith(' (30)')) {
        final msg = e.nativeMsg +
            " - this usually indicates a problem with permissions; if you're using Flutter you may need to use " +
            'getApplicationDocumentsDirectory() from the path_provider package, see example/README.md';
        throw ObjectBoxException(
            dartMsg: e.dartMsg, nativeCode: e.nativeCode, nativeMsg: msg);
      }
    }
    rethrow;
  }
}