从 Hive box 中删除的对象仍然加载到内存中
Objects deleted from Hive box are still loaded to memory
在我的 Flutter 应用程序中,我有一个带有 Player 对象的 Hive 框。只是为了调试,我在构造函数中从播放器对象输出一些数据,如下所示:
Player({
@required String name,
@required this.id,
}) {
this.name = name.toUpperCase();
print('${this.toString()}');
}
@override
String toString() {
return 'id: $id, name: $name';
}
玩家是这样添加到盒子中的,其中player.id是唯一键:
playerBox.put(player.id, player);
启动应用程序时,我还打印了 playerBox 的值:
print (playerBox.values);
这给了我所有添加的玩家。到目前为止,一切都很好。
但是……删掉一个播放器后是这样的:
playerBox.delete(playerId);
它开始表现得有点奇怪。当我重新启动应用程序时,已删除的播放器不再位于 playerBox.values 中,因此显然已从那里删除。但是所有被删除的玩家对象的构造函数仍然是 运行.
检查堆栈我可以看到这些对象确实是由PlayerAdapter 实例化的。因此,出于某种原因,Hive 仍会从磁盘中读取已删除的对象,但它们不在框中。
** 编辑
我也查看了我模拟器上的player.hive文件,所有删除的模型其实都在文件里。即使它们已被删除并且不会返回 playerBox.values
任何想法可能是什么原因?这是预期的行为吗?
我自己回答。
Any ideas what the reason might be? Is this expected behaviour?
是的,是的。我想我应该更仔细地检查这些文档:Compaction
Hive is an append-only data store. When you change or delete a value,
the change is written to the end of the box file. Sooner or later, the
box file uses more disk space than it should. Hive may automatically
"compact" your box at any time to close the "holes" in the file.
所以我通过像这样删除后压缩框来解决这个问题:
playerBox.compact();
在我的 Flutter 应用程序中,我有一个带有 Player 对象的 Hive 框。只是为了调试,我在构造函数中从播放器对象输出一些数据,如下所示:
Player({
@required String name,
@required this.id,
}) {
this.name = name.toUpperCase();
print('${this.toString()}');
}
@override
String toString() {
return 'id: $id, name: $name';
}
玩家是这样添加到盒子中的,其中player.id是唯一键:
playerBox.put(player.id, player);
启动应用程序时,我还打印了 playerBox 的值:
print (playerBox.values);
这给了我所有添加的玩家。到目前为止,一切都很好。 但是……删掉一个播放器后是这样的:
playerBox.delete(playerId);
它开始表现得有点奇怪。当我重新启动应用程序时,已删除的播放器不再位于 playerBox.values 中,因此显然已从那里删除。但是所有被删除的玩家对象的构造函数仍然是 运行.
检查堆栈我可以看到这些对象确实是由PlayerAdapter 实例化的。因此,出于某种原因,Hive 仍会从磁盘中读取已删除的对象,但它们不在框中。
** 编辑
我也查看了我模拟器上的player.hive文件,所有删除的模型其实都在文件里。即使它们已被删除并且不会返回 playerBox.values
任何想法可能是什么原因?这是预期的行为吗?
我自己回答。
Any ideas what the reason might be? Is this expected behaviour?
是的,是的。我想我应该更仔细地检查这些文档:Compaction
Hive is an append-only data store. When you change or delete a value, the change is written to the end of the box file. Sooner or later, the box file uses more disk space than it should. Hive may automatically "compact" your box at any time to close the "holes" in the file.
所以我通过像这样删除后压缩框来解决这个问题:
playerBox.compact();