mirror of
git://jb55.com/damus
synced 2024-09-29 08:20:45 +00:00
nostrdb: close database when backgrounded
Otherwise iOS gets mad because we are holding onto a lockfile in a
shared container which is apparently not allowed.
Fixes: a1e6be214e
("Migrate NostrDB files to shared app group file container")
This commit is contained in:
parent
c7cc8df5ba
commit
da2bdad18d
@ -448,18 +448,25 @@ struct ContentView: View {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { obj in
|
||||||
|
print("📙 DAMUS ACTIVE NOTIFY")
|
||||||
|
try? damus_state.ndb.reopen()
|
||||||
|
}
|
||||||
.onChange(of: scenePhase) { (phase: ScenePhase) in
|
.onChange(of: scenePhase) { (phase: ScenePhase) in
|
||||||
|
guard let damus_state else { return }
|
||||||
switch phase {
|
switch phase {
|
||||||
case .background:
|
case .background:
|
||||||
print("📙 DAMUS BACKGROUNDED")
|
print("📙 DAMUS BACKGROUNDED")
|
||||||
|
Task { @MainActor in
|
||||||
|
damus_state.ndb.close()
|
||||||
|
}
|
||||||
break
|
break
|
||||||
case .inactive:
|
case .inactive:
|
||||||
print("📙 DAMUS INACTIVE")
|
print("📙 DAMUS INACTIVE")
|
||||||
break
|
break
|
||||||
case .active:
|
case .active:
|
||||||
print("📙 DAMUS ACTIVE")
|
print("📙 DAMUS ACTIVE")
|
||||||
guard let ds = damus_state else { return }
|
damus_state.pool.ping()
|
||||||
ds.pool.ping()
|
|
||||||
@unknown default:
|
@unknown default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -15,8 +15,23 @@ enum NdbSearchOrder {
|
|||||||
case newest_first
|
case newest_first
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
enum DatabaseError: Error {
|
||||||
|
case failed_open
|
||||||
|
|
||||||
|
var errorDescription: String? {
|
||||||
|
switch self {
|
||||||
|
case .failed_open:
|
||||||
|
return "Failed to open database"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class Ndb {
|
class Ndb {
|
||||||
let ndb: ndb_t
|
var ndb: ndb_t
|
||||||
|
let path: String?
|
||||||
|
let owns_db: Bool
|
||||||
|
var closed: Bool
|
||||||
|
|
||||||
static func safemode() -> Ndb? {
|
static func safemode() -> Ndb? {
|
||||||
guard let path = db_path ?? old_db_path else { return nil }
|
guard let path = db_path ?? old_db_path else { return nil }
|
||||||
@ -59,7 +74,7 @@ class Ndb {
|
|||||||
Ndb(ndb: ndb_t(ndb: nil))
|
Ndb(ndb: ndb_t(ndb: nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
init?(path: String? = nil, owns_db_file: Bool = true) {
|
static func open(path: String? = nil, owns_db_file: Bool = true) -> ndb_t? {
|
||||||
var ndb_p: OpaquePointer? = nil
|
var ndb_p: OpaquePointer? = nil
|
||||||
|
|
||||||
let ingest_threads: Int32 = 4
|
let ingest_threads: Int32 = 4
|
||||||
@ -102,7 +117,18 @@ class Ndb {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
self.ndb = ndb_t(ndb: ndb_p)
|
return ndb_t(ndb: ndb_p)
|
||||||
|
}
|
||||||
|
|
||||||
|
init?(path: String? = nil, owns_db_file: Bool = true) {
|
||||||
|
guard let db = Self.open(path: path, owns_db_file: owns_db_file) else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
self.path = path
|
||||||
|
self.owns_db = owns_db_file
|
||||||
|
self.ndb = db
|
||||||
|
self.closed = false
|
||||||
}
|
}
|
||||||
|
|
||||||
private static func migrate_db_location_if_needed() throws {
|
private static func migrate_db_location_if_needed() throws {
|
||||||
@ -144,6 +170,23 @@ class Ndb {
|
|||||||
|
|
||||||
init(ndb: ndb_t) {
|
init(ndb: ndb_t) {
|
||||||
self.ndb = ndb
|
self.ndb = ndb
|
||||||
|
self.path = nil
|
||||||
|
self.owns_db = true
|
||||||
|
self.closed = false
|
||||||
|
}
|
||||||
|
|
||||||
|
func close() {
|
||||||
|
self.closed = true
|
||||||
|
ndb_destroy(self.ndb.ndb)
|
||||||
|
}
|
||||||
|
|
||||||
|
func reopen() throws {
|
||||||
|
guard self.closed,
|
||||||
|
let db = Self.open(path: self.path, owns_db_file: self.owns_db) else {
|
||||||
|
throw DatabaseError.failed_open
|
||||||
|
}
|
||||||
|
|
||||||
|
self.ndb = db
|
||||||
}
|
}
|
||||||
|
|
||||||
func lookup_note_by_key_with_txn<Y>(_ key: NoteKey, txn: NdbTxn<Y>) -> NdbNote? {
|
func lookup_note_by_key_with_txn<Y>(_ key: NoteKey, txn: NdbTxn<Y>) -> NdbNote? {
|
||||||
@ -344,12 +387,14 @@ class Ndb {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func process_event(_ str: String) -> Bool {
|
func process_event(_ str: String) -> Bool {
|
||||||
|
guard !closed else { return false }
|
||||||
return str.withCString { cstr in
|
return str.withCString { cstr in
|
||||||
return ndb_process_event(ndb.ndb, cstr, Int32(str.utf8.count)) != 0
|
return ndb_process_event(ndb.ndb, cstr, Int32(str.utf8.count)) != 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func process_events(_ str: String) -> Bool {
|
func process_events(_ str: String) -> Bool {
|
||||||
|
guard !closed else { return false }
|
||||||
return str.withCString { cstr in
|
return str.withCString { cstr in
|
||||||
return ndb_process_events(ndb.ndb, cstr, str.utf8.count) != 0
|
return ndb_process_events(ndb.ndb, cstr, str.utf8.count) != 0
|
||||||
}
|
}
|
||||||
@ -387,7 +432,7 @@ class Ndb {
|
|||||||
}
|
}
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
ndb_destroy(ndb.ndb)
|
self.close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +29,14 @@ class NdbTxn<T> {
|
|||||||
self.inherited = true
|
self.inherited = true
|
||||||
} else {
|
} else {
|
||||||
self.txn = ndb_txn()
|
self.txn = ndb_txn()
|
||||||
let _ = ndb_begin_query(ndb.ndb.ndb, &self.txn)
|
let ok = ndb_begin_query(ndb.ndb.ndb, &self.txn) != 0
|
||||||
|
if !ok {
|
||||||
|
self.moved = false
|
||||||
|
self.txn = ndb_txn()
|
||||||
|
self.inherited = true
|
||||||
|
self.val = with(self)
|
||||||
|
return
|
||||||
|
}
|
||||||
Thread.current.threadDictionary["ndb_txn"] = self.txn
|
Thread.current.threadDictionary["ndb_txn"] = self.txn
|
||||||
self.inherited = false
|
self.inherited = false
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user