Skip to content

Commit cf0529b

Browse files
Isolate stream: explain clone use with isolates, add code examples (#8)
1 parent 7d61d34 commit cf0529b

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

objectbox/lib/src/native/query/query.dart

+11
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,17 @@ class Query<T> {
724724
}
725725

726726
/// Clones this native query and returns a pointer to the clone.
727+
///
728+
/// This is useful to send a reference to a query to an isolate. A [Query] can
729+
/// not be sent to an isolate directly because it contains pointers.
730+
///
731+
/// ```dart
732+
/// // Clone the query and obtain its address, can be sent to an isolate.
733+
/// final queryPtrAddress = query._clone().address;
734+
///
735+
/// // Within an isolate re-create the query pointer to be used with the C API.
736+
/// final queryPtr = Pointer<OBX_query>.fromAddress(isolateInit.queryPtrAddress);
737+
/// ```
727738
Pointer<OBX_query> _clone() {
728739
final ptr = checkObxPtr(C.query_clone(_ptr));
729740
reachabilityFence(this);

objectbox/lib/src/native/store.dart

+25-3
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,12 @@ class Store {
227227
/// without any info like model definition, database directory and others.
228228
///
229229
/// This store is e.g. good enough to start a transaction, but does not allow
230-
/// to e.g. use boxes.
230+
/// to e.g. use boxes. This is useful when creating a store within another
231+
/// isolate as only information that can be sent to an isolate is necessary
232+
/// (the store and model definition contain pointers that can not be sent to
233+
/// an isolate).
231234
///
232-
/// Obtain a [ptrAddress] from [_clone].
235+
/// Obtain a [ptrAddress] from [_clone], see it for more details.
233236
Store._minimal(int ptrAddress, {bool queriesCaseSensitiveDefault = true})
234237
: _defs = null,
235238
_weak = false,
@@ -360,7 +363,26 @@ class Store {
360363

361364
/// Clones this native store and returns a pointer to the clone.
362365
///
363-
/// Use the address of the pointer with [Store._minimal].
366+
/// The address of the pointer can be used with [Store._minimal].
367+
///
368+
/// This can be useful to work with isolates as it is not possible to send a
369+
/// [Store] to an isolate (the Store itself and the contained model definition
370+
/// contain pointers). Instead, send the pointer address returned by this
371+
/// and create a minimal store (for limitations see [Store._minimal]) in the
372+
/// isolate. Make sure to [close] the clone as well before the isolate exits.
373+
///
374+
/// ```dart
375+
/// // Clone the store and obtain its address, can be sent to an isolate.
376+
/// final storePtrAddress = store.clone().address;
377+
///
378+
/// // Within an isolate create a minimal store.
379+
/// final store = InternalStoreAccess.createMinimal(isolateInit.storePtrAddress);
380+
/// try {
381+
/// // Use the store.
382+
/// } finally {
383+
/// store.close();
384+
/// }
385+
/// ```
364386
Pointer<OBX_store> _clone() {
365387
final ptr = checkObxPtr(C.store_clone(_ptr));
366388
reachabilityFence(this);

0 commit comments

Comments
 (0)