So I am using hive on Android. I have an adapter that is basically a class of 3 strings. Since the content of the box is added by the user (like a Contacts app where you add contacts) the user could be saving too many stuff. Since I load my box in main, is there any way I could limit the number of stuff stored inside a box? Like if the user saved 250 contacts and the maximum is 200 then start removing the old 50 contacts.
My second question would be how much is too much?
I tried using lazy boxes but it needs a future builder which messes my grid view builder
First of all, phones are extremely powerful and something like 200 contacts shouldn't be a problem. In fact, in most phones, memory limitations are very rare to come across today, unless you have some memory leak or deliberately try to exceed the memory limit (or you do something hefty and memory-intensive like video streaming, calculating chess games, training AIs, etc.). Saving contacts (even hundreds, thousands, tens of thousands) shouldn't be a problem.
From a UX perspective, I would also strongly argue against introducing a hard limit in the app. If the user decides to store millions of contacts or uses the content of whole books as contact names, longer startup times are something the user probably expects. The app is degrading gracefully in this case, rather than having a hard limit (which makes users try workarounds like merging unrelated contacts into one with multiple phone numbers).
If huge data volumes regularly slow down the startup of your app, there are things you can do:
LazyBox. This means, you'll need to await calls, but only the keys are loaded into memory.Hive.openBox(…) only when navigating to that page.If you still want to limit the number of entries in boxes (btw, I believe there are valid reasons to do that, like limiting the storage for a free version and increasing the limit in a pro version of your app), you should probably write a wrapper class around the box, something like a LimitedBox. When adding data, this box could check how many items it has and remove the oldest item.
Btw: If you implement a LimitedBox and maybe also an extension method for using Hive.limitedBox(…), don't hesitate to publish it as a package on pub.dev for other developers to use. They'll appreciate your effort and you'll be able to more easily reuse your code in future projects.
Thanks @marcelgarus for the great response! I think we can close this issue.
Most helpful comment
First of all, phones are extremely powerful and something like 200 contacts shouldn't be a problem. In fact, in most phones, memory limitations are very rare to come across today, unless you have some memory leak or deliberately try to exceed the memory limit (or you do something hefty and memory-intensive like video streaming, calculating chess games, training AIs, etc.). Saving contacts (even hundreds, thousands, tens of thousands) shouldn't be a problem.
From a UX perspective, I would also strongly argue against introducing a hard limit in the app. If the user decides to store millions of contacts or uses the content of whole books as contact names, longer startup times are something the user probably expects. The app is degrading gracefully in this case, rather than having a hard limit (which makes users try workarounds like merging unrelated contacts into one with multiple phone numbers).
If huge data volumes regularly slow down the startup of your app, there are things you can do:
LazyBox. This means, you'll need toawaitcalls, but only the keys are loaded into memory.Hive.openBox(…)only when navigating to that page.If you still want to limit the number of entries in boxes (btw, I believe there are valid reasons to do that, like limiting the storage for a free version and increasing the limit in a pro version of your app), you should probably write a wrapper class around the box, something like a
LimitedBox. When adding data, this box could check how many items it has and remove the oldest item.Btw: If you implement a
LimitedBoxand maybe also an extension method for usingHive.limitedBox(…), don't hesitate to publish it as a package on pub.dev for other developers to use. They'll appreciate your effort and you'll be able to more easily reuse your code in future projects.