Store.attach constructor

Store.attach(
  1. ModelDefinition modelDefinition,
  2. String? directoryPath, {
  3. bool queriesCaseSensitiveDefault = true,
})

Attach to a store opened in the directoryPath (or if null the defaultDirectoryPath).

Use this to access an open store from other isolates. This results in each isolate having access to the same underlying native store.

The returned store is a new instance (e.g. different pointer value) with its own lifetime and must also be closed (e.g. before an isolate exits). The actual underlying store is only closed when the last store instance is closed (e.g. when the app exits).

Implementation

Store.attach(ModelDefinition modelDefinition, String? directoryPath,
    {bool queriesCaseSensitiveDefault = true})
    : _closesNativeStore = true,
      _absoluteDirectoryPath = _safeAbsoluteDirectoryPath(directoryPath) {
  try {
    // Do not allow attaching to a store that is already open in the current
    // isolate. While technically possible this is not the intended usage
    // and e.g. transactions would have to be carefully managed to not
    // overlap.
    _checkStoreDirectoryNotOpen();

    final safeDirectoryPath = _safeDirectoryPath(directoryPath);
    withNativeString(safeDirectoryPath, (cStr) {
      if (debugLogs) {
        final isOpen = C.store_is_open(cStr);
        print('Attaching to store... path=$safeDirectoryPath isOpen=$isOpen');
      }
      _cStore = C.store_attach(cStr);
    });

    checkObxPtr(_cStore,
        'could not attach to the store at given path - please ensure it was opened before');

    // Not setting _reference as this is a replacement for obtaining a store
    // via reference.

    _attachConfiguration(_cStore, modelDefinition, safeDirectoryPath,
        queriesCaseSensitiveDefault);
    _attachFinalizer();
  } catch (e) {
    _readPointers.clear();
    rethrow;
  }
}