Skip to content

Commit 153d5bb

Browse files
Note Dart 2.15 is required for runIsolated.
1 parent c521dd6 commit 153d5bb

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

objectbox/CHANGELOG.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
* Support [ObjectBox Admin](https://docs.objectbox.io/data-browser) for Android apps to browse
44
the database. #148
5-
* Add `Store.runIsolated` to run database operations (asynchronous) in the background. It spawns an
6-
isolate, runs the given callback in that isolate with its own Store and returns the result of the
7-
callback. This is similar to Flutters compute, but with the callback having access to a Store.
5+
* Add `Store.runIsolated` to run database operations (asynchronous) in the background
6+
(requires Flutter 2.8.0/Dart 2.15.0 or newer). It spawns an isolate, runs the given callback in that
7+
isolate with its own Store and returns the result of the callback. This is similar to Flutters
8+
compute, but with the callback having access to a Store. #384
89
* Add `Store.attach` to attach to a Store opened in a directory. This is an improved replacement for
910
`Store.fromReference` to share a Store across isolates. It is no longer required to pass a
1011
Store reference and the underlying Store remains open until the last instance is closed. #376

objectbox/lib/src/native/store.dart

+3
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,9 @@ class Store {
399399
///
400400
/// Instances of [callback] must be top-level functions or static methods
401401
/// of classes, not closures or instance methods of objects.
402+
///
403+
/// Note: this requires Dart 2.15.0 or newer
404+
/// (shipped with Flutter 2.8.0 or newer).
402405
Future<R> runIsolated<P, R>(
403406
TxMode mode, FutureOr<R> Function(Store, P) callback, P param) async {
404407
final resultPort = ReceivePort();

objectbox/test/basics_test.dart

+16-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'dart:io';
44
import 'dart:isolate';
55

66
import 'package:async/async.dart';
7+
import 'package:meta/meta.dart';
78
import 'package:objectbox/internal.dart';
89
import 'package:objectbox/src/native/bindings/bindings.dart';
910
import 'package:objectbox/src/native/bindings/helpers.dart';
@@ -200,7 +201,21 @@ void main() {
200201
final futureResult =
201202
env.store.runIsolated(TxMode.write, readStringAndRemove, id);
202203
print('Count in main isolate: ${env.box.count()}');
203-
final x = await futureResult;
204+
final String x;
205+
try {
206+
x = await futureResult;
207+
} catch (e) {
208+
final dartVersion = RegExp('([0-9]+).([0-9]+).([0-9]+)')
209+
.firstMatch(Platform.version)
210+
?.group(0);
211+
if (dartVersion != null && dartVersion.compareTo('2.15.0') < 0) {
212+
print('runIsolated requires Dart 2.15, ignoring error.');
213+
env.closeAndDelete();
214+
return;
215+
} else {
216+
rethrow;
217+
}
218+
}
204219
expect(x, 'foo!');
205220
expect(env.box.count(), 0); // Must be removed once awaited
206221
env.closeAndDelete();

0 commit comments

Comments
 (0)