Open ThatPham2000 opened 3 months ago
Is this maybe because the ToMany
b.c
is loaded on first access?
If not, then the relation of "b" is not updated. I would welcome a simple example project to reproduce it.
Anyhow, I strongly recommend to use a separate model for JSON and for ObjectBox and map between them. Otherwise, future changes may be difficult or impossible.
Also: please don't post screenshots, post actual code.
Also: I provided all of my code. You can copy and run.
Oh, sorry I did not see the collapsed code block. I updated the description with the actual code that can be copied. I will have a look once I have time.
Anyhow, my question is still valid. Including the note about model separation.
The problem is that my model (not mock ones here) is built based on API so I need to parse from JSON here.
The problem is that my model (not mock ones here) is built based on API so I need to parse from JSON here.
Sure. But your project should then still have a separate model for the database. So one for JSON parsing and one for the database. Then map between those.
The underlying issue is that the constructor for B
accesses the ToMany
:
B({required this.c}) {
obId = c.map((element) => element).toList().hashCode;
}
This breaks the ToMany
as it is initialized only after this. See the generated code:
final cParam = obx.ToMany<C>();
final object = B(c: cParam)
..obId =
const fb.Int64Reader().vTableGetNullable(buffer, rootOffset, 4);
obx_int.InternalToManyAccess.setRelInfo<B>(
object.c, store, obx_int.RelInfo<B>.toMany(1, object.obId!));
return object;
We might be able to change the generated code to call setRelInfo
before passing the ToMany to the object.
Thanks for your update. so what is the solution for duplicate objects in case I do not assign object box id by myself (assignable: true). Because when I call the API many times, we have a lot of duplicate objects with the same values.
The above issue only exists when reading from a Box. The data is correctly put. Maybe you can change the model to provide a default, no arguments constructor that ObjectBox can use.
Or as I said, do not use the same model for your network layer (JSON) and the database layer.
Is there an existing issue?
No
Build info
Steps to reproduce
Just run this code:
Expected behavior
I want to get a list of "c" from "a".
Actual behavior
Get "c" from "a" is empty.
Code
Code
```dart import 'package:json_annotation/json_annotation.dart'; import 'package:objectbox/objectbox.dart'; part 'model.g.dart'; @Entity() @JsonSerializable(explicitToJson: true) class A { A({required this.name, required this.b}) { obId = name.hashCode; } @Id(assignable: true) @JsonKey(includeFromJson: false, includeToJson: false) int? obId; final String name; @_ToOneConverter() final ToOne b; factory A.fromJson(MapLogs, stack traces
TODO Add relevant logs, a stack trace or crash report.
Logs
```console [Paste your logs here] ```