isar / isar/hive

Can I keep Hive boxes synced between threads?

Open
#561 13 comments 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'm making a music app that does the music login on one thread and the rest of the app is run on another. In the app, I let users download songs locally. I'm using Hive to keep track of what songs are downloaded. I'm running into an issue where if the user downloads a song, the box on the music thread won't get these changes, causing the music thread to assume the item is not downloaded. The music is made using audio_service.

Is there a way for me to tell the box to sync itself with whatever is on the disk?

**Code sample**
On the main thread:
```dart
Box downloadedItemsBox = Hive.box("DownloadedItems");
...
Future addDownloads(...) {
...
// Adds the current song to the downloaded items box with its media info and download id
downloadedItemsBox.put(item.id, songInfo);
...
}
```
On the music thread:
```dart
Box downloadedItemsBox = Hive.box("DownloadedItems");
...
Future addItemToQueue(...) {
...
if (_downloadedItemsBox.containsKey(mediaItem.id)) {...}
...
}
```
If the music thread is running, the new item may not show in the music thread and the if statement will return false, even though the item has been added to the box.

**Version**
- Platform: Android
- Flutter version: 1.22.6
- Hive version: 1.4.4

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.