Skip to content

Commit 7efb031

Browse files
Fix ID generation for Box.putMany
1 parent 9f8f456 commit 7efb031

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

objectbox/lib/src/box.dart

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -243,32 +243,29 @@ class Box<T> {
243243
++numInstsMissingId;
244244

245245
// generate new IDs for these instances and set them
246-
Pointer<Uint64> instIdsMemory;
246+
Pointer<Uint64> firstIdMemory;
247247
if(numInstsMissingId != 0) {
248-
instIdsMemory = Pointer<Uint64>.allocate(count: numInstsMissingId);
249-
checkObx(bindings.obx_box_ids_for_put(_objectboxBox, numInstsMissingId, instIdsMemory));
250-
int newIdIndex = 0;
248+
firstIdMemory = Pointer<Uint64>.allocate(count: 1);
249+
checkObx(bindings.obx_box_ids_for_put(_objectboxBox, numInstsMissingId, firstIdMemory));
250+
int nextId = firstIdMemory.load<int>();
251+
firstIdMemory.free();
251252
for(var instPropVals in allPropVals)
252253
if(instPropVals[idPropName] == null || instPropVals[idPropName] == 0)
253-
instPropVals[idPropName] = instIdsMemory.elementAt(newIdIndex++).load<int>();
254+
instPropVals[idPropName] = nextId++;
254255

255256
}
256257

257258
// because obx_box_put_many also needs a list of all IDs of the elements to be put into the box, generate this list now (only needed if not all IDs have been generated)
258-
if(numInstsMissingId != insts.length) {
259-
if(instIdsMemory != null)
260-
instIdsMemory.free();
261-
instIdsMemory = Pointer<Uint64>.allocate(count: insts.length);
262-
for(int i = 0; i < allPropVals.length; ++i)
263-
instIdsMemory.elementAt(i).store(allPropVals[i][idPropName]);
264-
}
259+
Pointer<Uint64> allIdsMemory = Pointer<Uint64>.allocate(count: insts.length);
260+
for(int i = 0; i < allPropVals.length; ++i)
261+
allIdsMemory.elementAt(i).store(allPropVals[i][idPropName]);
265262

266263
// marshal all objects to be put into the box
267264
var putObjects = _ByteBufferArray(allPropVals.map(_marshal).toList()).toOBXBytesArray();
268265

269-
checkObx(bindings.obx_box_put_many(_objectboxBox, putObjects.ptr, instIdsMemory, _getOBXPutMode(mode)));
266+
checkObx(bindings.obx_box_put_many(_objectboxBox, putObjects.ptr, allIdsMemory, _getOBXPutMode(mode)));
270267
putObjects.free();
271-
instIdsMemory.free();
268+
allIdsMemory.free();
272269
return allPropVals.map((p) => p[idPropName] as int).toList();
273270
}
274271

0 commit comments

Comments
 (0)