Introduction
I have been doing a small project in Python for a while now. It's been called Sitemarker. It is live in the Flathub app store. It's been fun and all but recently, I've been busy getting it rewritten in Flutter.
Why Flutter?
I've been trying to get the entire program run on different platforms. First I tried web technologies to get it done. Sadly enough, my experience on the Web was not enough for the task. And, I was interested in learning Flutter.
The core things
I could get the entire code written in a few weeks. The UI was fine, and routings were doing good, until
Deletion of records
Any insertion must have a deletion. It's a thing. However, in my case, every insertion did have a deletion until the application closed.
The issue
Isar must have compaction to update the database to remove stale records. As with any great library, isar had one issue on the topic. Documentation.
I'm not going to lie. Isar has good if not great, documentation. The issue I found though, is that there is literally no guide for compacting an isar database. And I am new to local databases. MySQL did a good job making me comfortable with it. And I was practically close to completing the entire code.
The Workaround.
The workaround is a trick of sorts. When the deletion is called, it first deletes from the application runtime records. Then, said records that did not have the record to be deleted were copied to a new variable. The database now is closed, with deletion enabled on database closure.
The code
void deleteRecord(DBRecord record) async {
final isar = await db;
await isar!.writeTxn(() async {
isar.dBRecords.delete(record.id);
_records.remove(record);
});
deleteWorkaround();
}
void deleteWorkaround() async {
final recs = _records; // Get records.
final isar = await db;
await isar!.close(deleteFromDisk: true); // Close the db. Delete the db.
final isarTemp = await openDB();
List<DBRecord> localRecords = [];
isarTemp!.txn(() async {
final dbrecordCollection = isarTemp.dBRecords;
localRecords = await dbrecordCollection.where().findAll();
});
for (int i = 0; i < recs.length; i++) {
await isarTemp.writeTxn(() async {
await isarTemp.dBRecords.put(recs[i]);
localRecords.add(recs[i]);
});
}
await isarTemp.close();
_records = [];
db = openDB();
init();
notifyListeners();
}
The notifyListeners();
function is part of the provider
package for state management.
Top comments (0)