@@ -5,6 +5,7 @@ import 'common.dart';
55import "store.dart" ;
66import "bindings/bindings.dart" ;
77import "bindings/constants.dart" ;
8+ import "bindings/data_visitor.dart" ;
89import "bindings/flatbuffers.dart" ;
910import "bindings/helpers.dart" ;
1011import "bindings/structs.dart" ;
@@ -24,8 +25,9 @@ class Box<T> {
2425 ModelEntity _modelEntity;
2526 ObjectReader <T > _entityReader;
2627 OBXFlatbuffersManager <T > _fbManager;
28+ final bool _supportsBytesArrays;
2729
28- Box (this ._store) {
30+ Box (this ._store) : _supportsBytesArrays = bindings. obx_supports_bytes_array () == 1 {
2931 EntityDefinition <T > entityDefs = _store.entityDef <T >();
3032 _modelEntity = entityDefs.model;
3133 _entityReader = entityDefs.reader;
@@ -160,34 +162,63 @@ class Box<T> {
160162 }
161163 }
162164
163- List <T > _getMany (bool allowMissing, Pointer <OBX_bytes_array > Function () cCall) {
165+ List <T > _getMany (
166+ bool allowMissing, Pointer <OBX_bytes_array > Function () cGetArray, void Function (DataVisitor ) cVisit) {
164167 return _store.runInTransaction (TxMode .Read , () {
165- final bytesArray = cCall ();
166- try {
167- return _fbManager.unmarshalArray (bytesArray, allowMissing: allowMissing);
168- } finally {
169- bindings.obx_bytes_array_free (bytesArray);
168+ if (_supportsBytesArrays) {
169+ final bytesArray = cGetArray ();
170+ try {
171+ return _fbManager.unmarshalArray (bytesArray, allowMissing: allowMissing);
172+ } finally {
173+ bindings.obx_bytes_array_free (bytesArray);
174+ }
175+ } else {
176+ final results = < T > [];
177+ final visitor = DataVisitor ((Pointer <Uint8 > dataPtr, int length) {
178+ if (dataPtr == null || dataPtr.address == 0 || length == 0 ) {
179+ if (allowMissing) {
180+ results.add (null );
181+ return true ;
182+ } else {
183+ throw Exception ('Object not found' );
184+ }
185+ }
186+ final bytes = dataPtr.asTypedList (length);
187+ results.add (_fbManager.unmarshal (bytes));
188+ return true ;
189+ });
190+
191+ try {
192+ cVisit (visitor);
193+ } finally {
194+ visitor.close ();
195+ }
196+ return results;
170197 }
171198 });
172199 }
173200
174201 /// Returns a list of [ids.length] Objects of type T, each corresponding to the location of its ID in [ids] .
175- /// Non-existant IDs become null.
202+ /// Non-existent IDs become null.
176203 List <T > getMany (List <int > ids) {
177204 if (ids.isEmpty) return [];
178205
179- const bool allowMissing = true ; // returns null if null is encountered in the data found
206+ const bool allowMissing = true ; // result includes null if an object is missing
180207 return OBX_id_array .executeWith (
181208 ids,
182- (ptr) => _getMany (allowMissing,
183- () => checkObxPtr (bindings.obx_box_get_many (_cBox, ptr), "failed to get many objects from box" )));
209+ (ptr) => _getMany (
210+ allowMissing,
211+ () => checkObxPtr (bindings.obx_box_get_many (_cBox, ptr), "failed to get many objects from box" ),
212+ (DataVisitor visitor) => checkObx (bindings.obx_box_visit_many (_cBox, ptr, visitor.fn, visitor.userData))));
184213 }
185214
186215 /// Returns all stored objects in this Box.
187216 List <T > getAll () {
188217 const bool allowMissing = false ; // throw if null is encountered in the data found
189218 return _getMany (
190- allowMissing, () => checkObxPtr (bindings.obx_box_get_all (_cBox), "failed to get all objects from box" ));
219+ allowMissing,
220+ () => checkObxPtr (bindings.obx_box_get_all (_cBox), "failed to get all objects from box" ),
221+ (DataVisitor visitor) => checkObx (bindings.obx_box_visit_all (_cBox, visitor.fn, visitor.userData)));
191222 }
192223
193224 /// Returns a builder to create queries for Object matching supplied criteria.
0 commit comments