Open oligazar opened 3 years ago
For the time being, sqflite is not cross isolate safe. I don't recommend using it from another isolate as it can bring issues like the one you encounter.
@alextekartik Thanks for the quick reply! Do you happen to know any isolate-safe database implementation as an alternative? Or any other storage solution for that matter? May having two instances of the db in both isolates be a solution/workaround?
@oligazar I am not aware of any easy solution, sorry.
@oligazar https://moor.simonbinder.eu/docs/advanced-features/isolates/
Thanks a lot! Looks very promising!
Just out of curiosity, what are you using isolates for?
The typical reason is that running expensive cpu-bound work on the main UI isolate causes frame drops, so you move that work to a non-UI isolate. It takes longer to communicate over the isolate, so you trade off a bit of overall speed for a smoother UI.
This is pretty much the setup that sqflite already has: the expensive cpu-bound work (using the sqlite api to execute the statement and possibly step through the results) is done in a native thread and communicated over a method channel. This means the only way sqflite should cause frame drops is if you're receiving a very large amount of data over the method channel (where "very large" normally means "more data than should reasonably be displayed on one screen").
Moor, on the other hand, has the option to interact with the sqlite api directly using dart:ffi. In this case, since the expensive cpu-bound work is done directly in Dart, it makes sense to move that work to a separate isolate.
I haven't benchmarked yet, but I'd be curious to know:
I'm getting "database is locked" exception. I don't use any transactions in my code though.
Methods mentioned in the error trace:
One thing that may be important here is that 'deleteAllFrom' and '_insertMap' are called from different isolates. Also there wasn't huge insert/updates when the lock happened.
Is there any way to avoid the blocking or resolve the issue in some other way?