ndb/txn: make transactions failable

Since there may be situations where we close and re-open the database,
we need to make sure transactions fail when the database is not open.

Make NdbTxn an init?() constructor and check for ndb.closed. If it's
closed, then fail transaction construction.

This fixes crashes during high database activity when switching from
background to foreground and vice-versa.

Fixes: da2bdad18d ("nostrdb: close database when backgrounded")
This commit is contained in:
William Casarin committed 2024-01-10 14:27:02 -08:00
1 parent 227734d286
commit bfad2ab42d
42 files changed
+126 -107

No files matched your search

@@ -36,7 +36,7 @@ class SuggestedUsersViewModel: ObservableObject {
func suggestedUser(pubkey: Pubkey) -> SuggestedUser? {
let profile_txn = damus_state.profiles.lookup(id: pubkey)
if let profile = profile_txn.unsafeUnownedValue,
if let profile = profile_txn?.unsafeUnownedValue,
let user = SuggestedUser(name: profile.name, about: profile.about, picture: profile.picture, pubkey: pubkey) {
return user
}