1

In project i use latest Dart version with objectbox: ^1.0.0:

@Entity
class Node{

 ...
  @Transient()
  final List<Edge> _edges = List<Edge>.empty(growable: true);
 
  final relEdges = ToMany<Edge>();
 ...

}

@Entity
class Edge{
 ...

  @Transient()
  final List<Node> _nodes = List<Node>.empty(growable: true);

  @Backlink()
  final relNodes = ToMany<Node>();
 ...
}

after nodes and edges are created. Nodes are assigned to the list of nodes in edge object(reverse in nodeObject) and then in DAO layer they are (re)applied to relList(ToMany) of objectbox.

Actual put:

main_test.dart:

 // nodes and edge are created
 node1.dao.create(node1);
 node2.dao.create(node2);
 node3.dao.create(node3);
 edge..nodes.addAll([node1,node2]);
 edge.dao.create(edge);
 node1.dao.update(node1..edges.add(edge));
 node2.dao.update(node2..edges.add(edge));
 ...
 // removes edge from node2's edge list
 node2..edges.removeWhere((element) {
  return element.uuid == edge.uuid;
 });
 node3..edges.add(edge);
 // this placement also didn't change anything
 //await node2.dao.update(node2);
 //await node3.dao.update(node3);
 // or remove(1) and add(node3)
 edge..edges.clear();
 edge..edges.addAll([node1,node3]);

 await node2.dao.update(node2);
 await node3.dao.update(node3);
 await node1.dao.update(node1);
 await edge.dao.update(edge)
 ...

edgeDao.dart

    ...
    element.relNodes.clear();
    element.relNodes.addAll(edges);
    ...

nodeDao.dart

    ...
    element.relEdges.clear();
    element.relEdges.addAll(nodes);
    ...

databaseConnector.dart:

 // Box<(Element)> _box; is alrady initialized
 ...
 // it is implemented the same way, for Edge class
 // create works the same way
 Future<void> update(Node element) async {
    this._box.put(element);
  }
 ...

add operation works properly, just like update, before i try to save changes of edge relations, after old node is removed from ToMany and new one is added (this crashes every further put operation). I get following error from objectbox (x3):

>package:objectbox/src/native/bindings/helpers.dart 78:9                                                        ObjectBoxNativeError.throwMapped
>package:objectbox/src/native/bindings/helpers.dart 50:48                                                       throwLatestNativeError
>package:objectbox/src/native/bindings/helpers.dart 17:5                                                        checkObx
>package:objectbox/src/native/box.dart 461:7                                                                    InternalBoxAccess.relRemove
>package:objectbox/src/relations/to_many.dart 195:33                                                            ToMany.applyToDb.<fn>
>dart:collection                                                                                                _LinkedHashMapMixin.forEach
>package:objectbox/src/relations/to_many.dart 168:15                                                            ToMany.applyToDb
>package:objectbox/src/native/box.dart 365:13                                                                   Box._putToManyRelFields.<fn>
>dart:collection                                                                                                _LinkedHashMapMixin.forEach
>package:objectbox/src/native/box.dart 362:37                                                                   Box._putToManyRelFields
> ...

>ObjectBoxException: 404 404: Unknown native error

neither put() nor applyToDB() work. I even tried to use clear() and then to addAll to ToMany from list object. Any suggestions why this happens?

5
  • Thanks for reporting. Can you share the actual code that does the put? Also looking at the examples for this may help: docs.objectbox.io/relations Sep 6, 2021 at 5:28
  • So the actual issue is that when removing and then adding something to a relation, the put call fails? What if you also put after the remove and before the add? Anyhow, as it appears you have a full example, would you mind sharing it so we can more easily reproduce this? You can also create an issue at github.com/objectbox/objectbox-dart/issues. Sep 13, 2021 at 10:48
  • When i try to add new record only after i've done remove+update operation , it works fine. So the only problem is when remove and update are run at the same time. Unfortunately i can't share a full example, it's only a tiny part of my whole project . Sep 17, 2021 at 10:20
  • Actually, removing and adding before a single put works: added a test at github.com/objectbox/objectbox-dart/commit/… Can you double check your code? Note that you do not need to update the backlink ToMany. It is updated automatically when the ToMany it points to changes. Or vice versa. Sep 21, 2021 at 10:33
  • I'm going to check, why this operation doesn't work in my tests and when i find the answer, i'll add it here. Thank you for your help. Sep 22, 2021 at 11:23

1 Answer 1

0

In my case after remove, i have to update the ToMany relation. Only then i can add a new value. When i find the reason, why it these operations don't work in my tests, i'll update the answer.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Sep 17, 2021 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.