diff --git a/hive/CHANGELOG.md b/hive/CHANGELOG.md index 360b96e3..cd60a72a 100644 --- a/hive/CHANGELOG.md +++ b/hive/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.10.1 + +- Fixes `HiveObject` disposal in lazy boxes + ## 2.10.0 - Raises the maximum type ID from 223 to 65439 diff --git a/hive/lib/src/object/hive_object.dart b/hive/lib/src/object/hive_object.dart index b168d53d..c3a5f4bc 100644 --- a/hive/lib/src/object/hive_object.dart +++ b/hive/lib/src/object/hive_object.dart @@ -35,9 +35,13 @@ mixin HiveObjectMixin { } /// Deletes this object from the box it is stored in. - Future delete() { + Future delete() async { _requireInitialized(); - return _box!.delete(_key); + await _box!.delete(_key); + if (_box?.lazy == true) { + // Lazy boxes won't automatically dispose their HiveObjects + dispose(); + } } /// Returns whether this object is currently stored in a box. diff --git a/hive/pubspec.yaml b/hive/pubspec.yaml index 24c85468..793023e5 100644 --- a/hive/pubspec.yaml +++ b/hive/pubspec.yaml @@ -1,6 +1,6 @@ name: hive_ce description: Hive Community Edition - A spiritual continuation of Hive v2 -version: 2.10.0 +version: 2.10.1 homepage: https://github.com/IO-Design-Team/hive_ce/tree/main/hive documentation: https://docs.hivedb.dev/ diff --git a/hive/test/integration/hive_object_test.dart b/hive/test/integration/hive_object_test.dart index 8aeb1351..7e8abf27 100644 --- a/hive/test/integration/hive_object_test.dart +++ b/hive/test/integration/hive_object_test.dart @@ -72,4 +72,28 @@ void main() { }, timeout: longTimeout, ); + + test('move HiveObject between lazy boxes', () async { + final hive = await createHive(); + hive.registerAdapter<_TestObject>(_TestObjectAdapter()); + + final box1 = await openBox(true, hive: hive); + final box2 = await openBox(true, hive: hive); + + final obj = _TestObject('test'); + expect(obj.box, null); + expect(obj.key, null); + + final key1 = await box1.add(obj); + expect(obj.box, box1); + expect(obj.key, key1); + + await obj.delete(); + expect(obj.box, null); + expect(obj.key, null); + + final key2 = await box2.add(obj); + expect(obj.box, box2); + expect(obj.key, key2); + }); } diff --git a/hive/test/tests/box/box_base_test.dart b/hive/test/tests/box/box_base_test.dart index 6d408bdd..ff1c4a7c 100644 --- a/hive/test/tests/box/box_base_test.dart +++ b/hive/test/tests/box/box_base_test.dart @@ -13,6 +13,9 @@ import '../common.dart'; import '../mocks.dart'; class _BoxBaseMock extends BoxBaseImpl with Mock { + @override + final lazy = false; + _BoxBaseMock( super.hive, super.name, @@ -158,7 +161,6 @@ void main() { test('.initialize()', () async { final backend = MockStorageBackend(); final box = _openBoxBaseMock(backend: backend); - when(() => box.lazy).thenReturn(false); when(() => backend.initialize(any(), any(), any())).thenAnswer((i) async { i.positionalArguments[1].insert(Frame('key1', 1)); diff --git a/hive/test/tests/mocks.dart b/hive/test/tests/mocks.dart index f1e2722f..d93f8ba4 100644 --- a/hive/test/tests/mocks.dart +++ b/hive/test/tests/mocks.dart @@ -11,7 +11,12 @@ import 'package:mocktail/mocktail.dart'; // Mocks -class MockBox extends Mock implements Box {} +class MockBox extends Mock implements Box { + @override + final bool lazy; + + MockBox({this.lazy = false}); +} class MockChangeNotifier extends Mock implements ChangeNotifier {} diff --git a/hive/test/tests/object/hive_object_test.dart b/hive/test/tests/object/hive_object_test.dart index 58c501fd..c36cd6eb 100644 --- a/hive/test/tests/object/hive_object_test.dart +++ b/hive/test/tests/object/hive_object_test.dart @@ -153,7 +153,6 @@ void main() { test('returns true if object is in normal box', () { final obj = TestHiveObject(); final box = MockBox(); - when(() => box.lazy).thenReturn(false); obj.init('key', box); expect(obj.isInBox, true); @@ -162,8 +161,7 @@ void main() { test('returns the result ob box.containsKey() if object is in lazy box', () { final obj = TestHiveObject(); - final box = MockBox(); - when(() => box.lazy).thenReturn(true); + final box = MockBox(lazy: true); obj.init('key', box); when(() => box.containsKey('key')).thenReturn(true); diff --git a/hive_flutter/test/mocks.mocks.dart b/hive_flutter/test/mocks.mocks.dart index 8ceaa07a..7693c486 100644 --- a/hive_flutter/test/mocks.mocks.dart +++ b/hive_flutter/test/mocks.mocks.dart @@ -338,8 +338,8 @@ class MockBinaryWriter extends _i2.Mock implements _i8.BinaryWriter { ); @override - void write(T? value, {bool? writeTypeId = true}) => super.noSuchMethod( - Invocation.method(#write, [value], {#writeTypeId: writeTypeId}), + void write(T? value, {bool? withTypeId = true}) => super.noSuchMethod( + Invocation.method(#write, [value], {#withTypeId: withTypeId}), returnValueForMissingStub: null, ); }