compute<T> static method

Future<T> compute<T>(
  1. FutureOr<T> computation(), {
  2. String? debugName,
})

Runs computation in a new isolate and returns the result. Also takes care of initializing Hive and closing all boxes afterwards.

The optional debugName can be used to identify the isolate in debuggers.

Implementation

static Future<T> compute<T>(
  FutureOr<T> Function() computation, {
  String? debugName,
}) {
  final registry = _typeRegistry;
  final dir = defaultDirectory;
  return Isolate.run(
    () async {
      Hive._typeRegistry = registry;
      Hive.defaultDirectory = dir;
      try {
        return await computation();
      } finally {
        Hive.closeAllBoxes();
      }
    },
    debugName: debugName ?? 'Hive Isolate',
  );
}