isar / isar/hive

Question to NEVER access a box from the compaction strategy.

Open
#1,095 1 comment 1 reaction 0 assignees View on GitHub
question
Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

**Question**
I had some issues with a project that just keeps increasing in storage due to the box (went up to 10gb). I had found a solution which is the compaction. The approach I did was this.

**Code sample**
```dart
void save(SomeData someData) async {
final box = await Hive.box('some_data_box');
await box.put(someData.id, someData);
await box.compact(); // Added this new line.
}
```

The code snippet above resolved my issue which just keeps the box from increasing in size.

> Note:
> - First I thought the `box.put` will replace the data of the data but it just keeps increasing.
> - Second I added a method to `box.delete` the key before adding a `put` but it didn't work. Didn't reduce the size at all hoping it would remove the data beforing adding.

Question though. Upon reading the documentation about the compaction it states:

> "NEVER access a box from the compaction strategy."

There was no explanation on why I should never access a box with this because I was planning to use the `compactionStrategy` instead of doing my code snipppet above.

**Version**
- Platform: iOS
- Flutter version: 2.10.0
- Hive version: ^1.1.0

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the compactionStrategy documentation and the box.compact() usage shown in the issue. Clarify why accessing a box during the compaction strategy is prohibited and document the safe alternative, then verify that the resulting guidance addresses the reported storage-growth scenario.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
database
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.