hive icon indicating copy to clipboard operation
hive copied to clipboard

Can't put object in the first position of the box

Open xOldeVx opened this issue 2 years ago • 5 comments

I created a box of history videos (Hive.openBox<VideoModel>(HISTORY_BOX)), when user watch video, the video is add to the history box, if the user watched the video before a week for example and watch it again today, i want to remove the video from a week ago, and put it first in the box, so the list can show this video today.

I have two operations to do, first to remove the video from the box, and the second is to add it again to the first position of the box.

To find the video i'm using get(videoId, null), but when i put the video again, it's putted in some position and not at the first position of the box.

How can i add the video to the first position of the box and still keeping on the videoId as a key of the saved video in the box?

xOldeVx avatar Sep 21 '22 15:09 xOldeVx

instead of adding and removing the entry, why not just simply have a date field that is updated with the last seen time? then when displaying it, sort on this field

BenWildeman avatar Sep 21 '22 18:09 BenWildeman

I didn't understand you completely, if you mean to use date as a key so i miss the videoId as a key, and i must it as a key. And if you mean to add a date field in my object, and after i get the box enteries then i sort them, i think it's a big drawback in the package, because i need to sort it after i fetch it instead of get it sorted, and i need to add an extra field in my object only for this sorting 😳🤯

xOldeVx avatar Sep 21 '22 19:09 xOldeVx

one of the issues with most databases is that you cannot guarantee order when stored on disk (I can't say for certain if Hive has this issue). for instance, most databases will order based on last insertion (whether it's an add or update) so applying a sort is pretty much expected to be implemented. for instance, in your app, say you have records that the user needs to see alphabetically, how would you expect to do this without using sort?

however, if applying a sort yourself with Hive isn't something you want to do, you may want to upgrade to Isar as this has sort as part of the query

BenWildeman avatar Sep 21 '22 19:09 BenWildeman

@BenWildeman Thanks for your answer. You right about Isar. when using Index it's can be a solution, but i left Isar because it's based on Models and not on Box, so when you need to create separate db for same model (like in my case, VideoModel has HistoryBox, FavoriteBox etc..), you must to duplicate the model and change the model name for your new box (like VideoModelFavorite.dart, VideoModelHistory.dart) , so it requires you to create the same classes only for create a new box..

xOldeVx avatar Sep 22 '22 05:09 xOldeVx

I found the solution, so Hive is support sorting when you create the box: keyComparator: (a, b) => -1 the sort is desc keyComparator: (a, b) => 1 the sort is asc

so Hive.openBox<String>('myBox',keyComparator:(a, b) => -1); solve my problem

xOldeVx avatar Sep 22 '22 07:09 xOldeVx