Skip to content

Commit

Permalink
fix: objectbox keyvalue save
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-lox committed Oct 31, 2024
1 parent 7df3d37 commit 2e9aaef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 18 additions & 1 deletion lib/data_layer/db/object_box_camelus/db_camelus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ class DbAppImpl implements AppDb {
@override
Future<void> save({required String key, required String value}) async {
await _dbRdy;
_objectBox.store.box<DbKeyValue>().put(DbKeyValue(key: key, value: value));

_objectBox.store.runInTransaction(TxMode.write, () {
// check if key already exists
final keyBox = _objectBox.store.box<DbKeyValue>();
final DbKeyValue? keyValue =
keyBox.query(DbKeyValue_.key.equals(key)).build().findFirst();
// update
if (keyValue != null) {
keyValue.value = value;
_objectBox.store.box<DbKeyValue>().put(keyValue, mode: PutMode.update);
} else {
// insert
final newKeyValue = DbKeyValue(key: key, value: value);
_objectBox.store
.box<DbKeyValue>()
.put(newKeyValue, mode: PutMode.insert);
}
});
}
}
2 changes: 1 addition & 1 deletion lib/presentation_layer/providers/main_feed_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class MainFeedState extends FamilyNotifier<FeedViewModel, String> {
} else {
cutoff = now;
}
//
// Save the current time as the new cutoff
appDbP.save(key: 'main_feed_cache_cutoff', value: now.toString());

// Timeline subscription
Expand Down

0 comments on commit 2e9aaef

Please sign in to comment.