-
Notifications
You must be signed in to change notification settings - Fork 411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hive Database Resets When Re-starting on Flutter Android Simulator #620
Comments
Currently Happens on iOS too, its a sudden development, I think it may be linked with flutter and dart update. Did you update yours? |
No. It happens randomly, without any DART/FLUTTER updates. One minute the db will be fine, and another restart a few minutes later all my user-prefs are gone because the .hive file no longer exists. I've tried stopping the app, cleaning project files, and the database is fine after these... sometimes. And then 20m later I do another restart and it's just gone. It's not consistent. I am at my wits end. It's making development frustrating. If I could be guaranteed that this was a simulator, development-only problem, I wouldn't worry as much. But I'm afraid when we start rolling out our app that this is going to happen to users. |
I'm having likely the same issue but mine does not tie to time. All data lost whenever I kill the app and re-open it. |
I am having the same issue in debug mode. Every time I restart the app and try to open the same box, it appears to be empty. |
I haven't noticed it happening in the production app. So maybe it is a simulator-only issue? |
I should also specify that this happens on Flutter v2.0.6 with sound null safety active, when running on my debug device (not simulator). |
facing the same issue in my android device (not simulated). Edit: I did not notice it on dart sdk < 2.7. |
I have same issue and after restart the app and call the box name got: |
One of my customer complains every now and then that she has lost data, this happens on iOs device. Unfortunately I cannot get more information about the case since it is not giving any errors, it seems like app is just reset. But from code perspective it looks like that whole box is gone missing. IMO this is pretty critical error for database. Some sort of guess is that it might be related to starting app from e.g. push notification. Same customer also hitted -> #192 or #263 so this might be somehow related to this as well |
If you need high reliability I would suggest using:
Don't get me wrong but all these tools had been around lots of years (SQLite was around for about 21 years and secure storage package uses APIs baked into operation system which was used by lots of apps). And store some data on server side if possible. It's very hard to create reliable database with disaster recovery, speed and ease use. Hive is mostly good about ease of use and performance, but has issues with reliability. |
I really like no-sql style approach here and overall Hive easiness compared to old school sql dbs. So lets see if I can give you some hints for stability. I just briefly checked through source, and seems like compact is done pretty much on end of writing etc. So it is first place for me to check if there is some possibilities for corruption. I checked only VM side of compact and there seems to be so that it writes compacted (or sorted) data to temporary file (which is good). But if I am right, writing happens in loop inside try catch, and even if there is any error (errors are silently ignored), it just renames temporary file as main file. Does this sound somewhat error prone to you? |
Wow @jutarhon , thanks for such a great insights. I'll look at them. But it's really hard to work on this issue without having a minimum reproducible example. It happens randomly and that's the issue. We (at least I hadn't) couldn't produce this issue on our machines. So I lost my hope on this issue because it took so much time without any progress. But I'll try to look at it in a few days again. |
I tried to reproduce customers problem, but as usual, it seems to be impossible... talked with customer and she said that problem occurs when app is opened via push message, and still quite rarely (of course). I also think about ios automatic app data backup, if there is some sort of locking for files, but this is just plain guessing. Most likely she does not have antivirus software, which seems to make some locking in some cases. |
We run into this in production and development as well. The data is on the filesystem. |
this problem have it to me in my Windows project sometime by randomly the database reset and i don't know why, i have no idea how to solve it. |
also happens on my side, anyone have solution? |
Phasing this issue in the real devices too. Any solutions? |
Did you got a solution to fix this? |
if anyone still having this issue i have a workaround |
@dragongesa what is the workaround? |
This is the only work version if you have auto_route, freezed and json_serializable together. if you still having this issue. please edit pubspect.lock by hand and forcing the version to match below. but it seems not work anymore in the latest flutter 3. im looking up for this again because i upgraded my flutter version. #DEP |
@jamesdixon did u try this workaround?
|
@themisir Don't get me wrong but it is really disappointing that you made this as a local database (for which high reliability is quite a very very very important feature), and now instead of owning up to it and try to fix the issue, you are recommending the developers to change their whole codebase that is built around this package. |
@waqadArshad man, this is an open source project powered by many members of the community and neither of us owe anything to anyone. We all do our best to provide whatever we can without expecting anything in return. I started contributing to hive because I used it myself and found a value in using it and had spare time for contribution. Regarding this issue, it's probably an edge case and without any way to reproduce it we have no way to find what's going on. There was similar case that a few months ago got fixed because someone on the community finally figured it out what's going on. Again, I can not provide any warranty that it's not going to fail as covered by the LICENSE.
First I would highly encourage EVERYONE to not build their project around ANY package. It's just a door to disaster. If you're building something for hobby, sure do it. But if you have long term goals, wrap it around with your own layer so you can switch gears when needed. Secondly I just provided recommendation because I thought it might be valuable. If you need reliability use something proven to be reliable (nothing beats time when it comes to proven reliability), if you wanna take risks sure do it. That DOESN'T means hive won't work, or would crash randomly. In my experience aside from #914 I didn't experienced any reliability issue with hive personally. However it's up to the developer to choose which way they would like to go. I can't just lie that it wouldn't ever fail. Even most proven reliable stuff from time to time gets broken, that's just nature of the software. (I spend multiple nights to learn importance of backups hard way, because our production postgres cluster got crashed and I had to manually recover it before our customers wake up. Yet nobody says "don't use postgres because it might fail"). Thirdly it's not an easy thing to build a reliable database. You're dealing with unknowns you're not even aware that might happen. Again if we see something wrong going on and if we have a way to reproduce it, we'll try to fixing it. If we can't we'll ask community for the help. Anyways, I hope you understand the situation. I know the feeling you're going thru - it sucks when things randomly break. But complaining doesn't help anyone. If you have a solution you can open up a PR and contribute to the package you've built your whole project around. EOF; |
@themisir really sorry about that, I just spent 3 to 4 days (of a tight deadline project) on fixing the bugs caused by this, without thinking that this could/would fail. and last night I realized that this might be the case and it is and that just made me really sad because, on top of those 3-4 days, I now need to put in at least 2 more to change it to sqflite and make sure that I change it everywhere and also make sure that it works. I am really sorry to have said that. and I'll try and see if I can help with this issue as soon as I have some free time. |
Hey @waqadArshad don't worry :) We've all been there. It's really stressful to work with tight deadlines. About the issue I'm sorry but I have no idea why it would reset by itself. Unless explicitly deleted the db should persist between restarts. I never experienced it myself, nor saw it happening on others who used hive. Aside from that I would generally advise to always encapsulate storage system with a separate layer. It's really not a good idea to access data directly from places where you would need them. |
When storing data it shows on the screen till I do not hot restart the app. Whenever I restart the app it shows an error. |
Facing similar issue,hive is not resetting but values inside model class is being changed to null. Steps to reproduce: I created a model
then
I tried box.put as and immedietly returned value using box.get and I got the value in ui. but after restart the key is there "1665301531499" but value got changed into flutter version is 3.3.4
|
same here. in my case - after hot reload file *.lock is removed. It happens in only 1 hive box (in my app I using 4 hive boxes) Edit: in my case i create manually adapter and in read method was error... Whats why box cant opened normally |
We are facing this issue, every morning. The Hive.box comes empty when we keep the phone idle for a very long time. This is very very sad.
Flutter Doctor: ! Doctor found issues in 1 category. |
Hive box becomes empty unexpectedly and it is happening on real devices as well. Please fix this on priority. Users are complaining frequently about this. |
Getting this issue every day on my iOS device, iPhone 12. Hive box becomes empty during long idle, every morning. |
@neeerajtk, @Hiren-fyers and @WiRight, did anyone of you find a solution / workaround? |
@Myzel394 in my case i close all hive boxes before closing app. And check my previous answer - I has been error in custom read method of adapter @override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.paused || state == AppLifecycleState.detached) {
hiveBox.compact();
}
} and ![]() in read method i use wrong reader.readX method and it will give exception and remove .lock files sorry for my bad english |
Finally, I've fixed the issue. Unfortunately, there is no any info about closing collection in the library usage description - https://stackoverflow.com/questions/68313808/flutter-hive-data-is-shown-after-restarting-the-app-on-android/76739626#76739626 |
To be precautious about this issue, is it recommended to close the Hive collection after every operation, whether it be a read or write? |
I also encountered this issue in my project, which was caused by incorrect reader and writer in the adapter. It took me about a day to identify the problem. This is because the first run does not produce an error after deleting the local directory's Hive files. Indeed, this issue is quite tricky. |
I too am having this issue. Its oddly consistently inconsistent. Right now it works perfectly for me on:
I install the same exact apk on my wife's Pixel 6 and she can never load items from memory on an app restart. |
Is the issue resolved now? |
Me too, after hot restart, I got error
No idea what this error means. I speculated that the box that I've created got deleted, and somehow Hive tried to delete existing path again. |
(Almost) every time we hard stop and do a full restart of our app on the Android Simulator, our Hive DB gets deleted. I'd say it's more often than not, but it is inconsistent. Is this a bug?
The text was updated successfully, but these errors were encountered: