Hive: Can I keep Hive boxes synced between threads?

Created on 13 Feb 2021  路  10Comments  路  Source: hivedb/hive

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:

Box<DownloadedSong> downloadedItemsBox = Hive.box("DownloadedItems");
...
Future<void> 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:

Box<DownloadedSong> downloadedItemsBox = Hive.box("DownloadedItems");
...
Future<void> 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
question

All 10 comments

My understanding is that you would need to close the box while updating it in the other thread. Since two applications can't use the same box.

@thevikke I am running into the same issue when storing notifications in the background which also runs in another isolate. i called Hive.close() before application goes to the background but upon app resuming data is still not found in the database until app is restarted

I don't quite understand the use case. What is the thing that runs in background and how? What notifications are you storing? Preferably give us a code example. :)

I don't quite understand the use case. What is the thing that runs in background and how? What notifications are you storing? Preferably give us a code example. :)

I am handling FirebaseMessaging.onBackgroundMessage.
`///!Firebase background handler
Future firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
await initHiveDB();
Get.put(NotificationController(
iNotificationRepository: NotificationRepository(
NotificationLocalDataSource(), NoticeCountLocalDataSource())));

await FCMMessagingHandlers.persistNotice(message.data);
await killHive();

return Future.value();
}`

I initialise Hive again and kill the boxes when it is done.

I would guess that there is something wrong in the initHiveDB or killHive function. 馃

Future<void> killHive() async { await Hive.box(BoxConstants.AUTHBOX).close(); await Hive.box(BoxConstants.NOTIFICATIONS_BOX).close(); await Hive.close(); }

Future initHiveDB() async { await Hive.initFlutter(); await Hive.openBox(BoxConstants.AUTHBOX); await Hive.openBox(BoxConstants.User_ACCOUNT_BOX); await Hive.openBox(BoxConstants.WEATHER_BOX); await Hive.openBox(BoxConstants.LANG_BOX); await Hive.openBox(BoxConstants.MACHINES_BOX); await Hive.openBox(BoxConstants.NOTIFICATIONS_BOX); await Hive.openBox(BoxConstants.NOTIFICATION_BADGE_BOX); await Hive.openBox(BoxConstants.PRESETUP_BOX); }

Not really sure whats wrong. Do you get any errors? Are you sure you are actually saving into the HiveDB? Do you need typeAdapters? I'm also not sure how Firebase works in the background, can you use initFlutter with it or do you need Hive.init?

Am not using TypeAdapters just normal Map<string,dynamic>. According to documentation Firebase in the background creates an Isolate. I use Hive.initFlutter. I don't get errors. Every code gets executed correctly.

@stilyng94 You solved the problem?

@stilyng94 You solved the problem?

I switched to Moor and everything is working fine. It has good support for background isolates

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rupamking1 picture rupamking1  路  3Comments

cachapa picture cachapa  路  4Comments

kaboc picture kaboc  路  3Comments

ProfileID picture ProfileID  路  4Comments

MyoLinOo picture MyoLinOo  路  3Comments