Closed phillwiggins closed 4 years ago
Indeed it is weird as it seems to try to access a root file ('/bc_repository.db' which is indeed readonly on most platforms) and it seems to fail after opening the database (or maybe it is failing due to a second call to _db)
For safety, I would ensure that _db
is called only once (using a lock or future, here there is an edge case where the database can be opened twice) - add a temp print (with the path) before opening the database for verification.
I'm not sure on which platform you are running the app though (Android? Linux?)
I am using Android in this instance. I will try using a lock now and come back to you shortly. Thanks for the quick response.
Yes, it does look like the path is printed, and I'm assuming the actual file is being created. As you mentioned, I think this might be a concurrent modification issue. Is there any functions I can use to assess wether my db is open already? I currently use a generic db read/write class that multiple repositories extend depending on the child object. Because of this I sometimes have more than one item being saved concurrently. Ideally, I wouldn't need to create a singleton DB, I should be able to check for the db being open and return when it's ready to be used.
One quick way to prevent _db to be called twice is to declare it this way:
final Future<Database> _db = () async {
final appDocDir = await getApplicationDocumentsDirectory();
await appDocDir.create(recursive: true);
final path = join(appDocDir.path, _dbKey);
final db = await _dbFactory.openDatabase(path, version: 1);
print('TESTDB: ${db.toString()}');
return db;
} ();
And no there is no isOpened
method, sorry (and anyway it is not alway safe and database object only exists when opened).
The path /data/user/0/com.purewowstudio.bodycal_new/app_flutter/
looks weird for android where it typically starts with /data/data/<package_name>
but I could be wrong on that.
I'll give that a go, but again I'm assuming if I have multiple repositories using this database, I would essentially need to create a singleton to store the instance of this DB. I suppose it should still work.
Thakns for your help.
Yes a singleton is a reasonable solution!
All sorted! Thanks for your help.
I have a generic database class that I extend for every object that is persistable, which all point to the same DB instance stored in a singleton. It seems to have solved my problem.
Thank you very much for your help!
I keep seeing this error when trying to write to my DB. I have seen in the past in an old issue but it's reappeared. I'm currently trying to write to a DB but hit the issue of (OS Error: Read-only file system, errno = 30). Any idea why?
My Sembast DB:-
Flutter Doctor (Im using AS)
My error output (You can see the print statement outputting something that looks like it has worked just before returning the created DB in my code snippet)
Any ideas what is going on? I'm a little lost on this one. Thanks in advance!