【问题标题】:Applying changes of ToMany<E> relation to objectbox store in dart将 ToMany<E> 关系的更改应用于 dart 中的 objectbox 存储
【发布时间】:2021-11-01 19:19:52
【问题描述】:

在项目中,我使用带有 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 层将它们(重新)应用于 objectbox 的 relList(ToMany)。

实际投入:

ma​​in_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 操作正常工作,就像更新一样,在我尝试保存边缘关系的更改之前,在旧节点从 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() 然后从列表对象 addAll 到 ToMany。有什么建议为什么会发生这种情况?

【问题讨论】:

  • 感谢您的报告。你能分享做put的实际代码吗?还查看示例可能会有所帮助:docs.objectbox.io/relations
  • @Uwe-ObjectBox 完成。
  • 所以实际的问题是当删除然后向关系中添加一些东西时,put 调用失败了?如果您还在删除之后和添加之前放置怎么办?无论如何,您似乎有一个完整的示例,您介意分享它以便我们更容易地复制它吗?您也可以在github.com/objectbox/objectbox-dart/issues 创建问题。
  • 当我在完成删除+更新操作后才尝试添加新记录时,它工作正常。所以唯一的问题是删除和更新同时运行。不幸的是我不能分享一个完整的例子,这只是我整个项目的一小部分。
  • 实际上,在单个 put 之前删除和添加工作:在 github.com/objectbox/objectbox-dart/commit/… 添加了一个测试你能仔细检查你的代码吗?请注意,您不需要更新反向链接 ToMany。当它指向的 ToMany 发生变化时,它会自动更新。反之亦然。

标签: dart flutter-test objectbox data-access-object flutter-objectbox


【解决方案1】:

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

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-10
  • 1970-01-01
相关资源
最近更新 更多