Merge pull request #1671 from famedly/krille/fix-delete-in-transactions

fix: Delete in transaction on new store does not clear cache correctly
This commit is contained in:
Krille-chan 2024-01-02 10:19:17 +01:00 committed by GitHub
commit dd31ede2a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 4 deletions

View File

@ -166,7 +166,10 @@ class Box<V> {
txn ??= boxCollection._db.transaction(name, 'readwrite');
final store = txn.objectStore(name);
await store.delete(key);
_cache.remove(key);
// Set to null instead remove() so that inside of transactions null is
// returned.
_cache[key] = null;
_cachedKeys?.remove(key);
return;
}
@ -183,7 +186,7 @@ class Box<V> {
final store = txn.objectStore(name);
for (final key in keys) {
await store.delete(key);
_cache.remove(key);
_cache[key] = null;
_cachedKeys?.remove(key);
}
return;

View File

@ -302,7 +302,9 @@ class Box<V> {
txn.delete(name, where: 'k = ?', whereArgs: [key]);
}
_cache.remove(key);
// Set to null instead remove() so that inside of transactions null is
// returned.
_cache[key] = null;
_cachedKeys?.remove(key);
return;
}
@ -326,7 +328,7 @@ class Box<V> {
}
for (final key in keys) {
_cache.remove(key);
_cache[key] = null;
_cachedKeys?.removeAll(keys);
}
return;

View File

@ -59,6 +59,18 @@ void main() {
await box.clear();
});
test('Box.delete in transaction', () async {
final box = collection.openBox<Map>('cats');
await box.put('fluffy', data);
await box.put('loki', data2);
await collection.transaction(() async {
await box.delete('fluffy');
expect(await box.get('fluffy'), null);
});
expect(await box.get('fluffy'), null);
await box.clear();
});
test('Box.deleteAll', () async {
final box = collection.openBox<Map>('cats');
await box.put('fluffy', data);