1

在项目中,我使用带有objectbox的最新 Dart 版本:^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>();
 ...
}

创建节点和边之后。节点被分配到边缘对象中的节点列表(在 nodeObject 中反向),然后在 DAO 层中,它们(重新)应用于对象框的relList(ToMany)。

实际看跌:

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);
    ...

节点道.dart

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

数据库连接器.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);
  }
 ...

添加操作正常工作,就像更新一样,在我尝试保存边缘关系的更改之前,在从 ToMany 中删除旧节点并添加新节点之后(这会导致每次进一步的 put 操作都崩溃)。我从 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

put() 和applyToDB()都不起作用。我什至尝试使用clear()然后将列表对象中的所有内容添加到 ToMany 中有什么建议为什么会发生这种情况?

4

1 回答 1

0

就我而言,删除后,我必须更新ToMany关系。只有这样我才能添加一个新值。当我找到原因时,为什么这些操作在我的测试中不起作用,我会更新答案。

于 2021-09-17T10:24:48.023 回答