Fix race condition on app swap that would cause ndb to remain closed
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
This commit is contained in:
@@ -135,6 +135,7 @@ struct ContentView: View {
|
||||
@StateObject var navigationCoordinator: NavigationCoordinator = NavigationCoordinator()
|
||||
@AppStorage("has_seen_suggested_users") private var hasSeenOnboardingSuggestions = false
|
||||
let sub_id = UUID().description
|
||||
@State var damusClosingTask: Task<Void, Never>? = nil
|
||||
|
||||
// connect retry timer
|
||||
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
|
||||
@@ -478,6 +479,8 @@ struct ContentView: View {
|
||||
}
|
||||
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { obj in
|
||||
print("txn: 📙 DAMUS ACTIVE NOTIFY")
|
||||
Task {
|
||||
await damusClosingTask?.value // Wait for the closing task to finish before reopening things, to avoid race conditions
|
||||
if damus_state.ndb.reopen() {
|
||||
print("txn: NOSTRDB REOPENED")
|
||||
} else {
|
||||
@@ -502,7 +505,6 @@ struct ContentView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
Task {
|
||||
await damus_state.purple.check_and_send_app_notifications_if_needed(handler: home.handle_damus_app_notification)
|
||||
}
|
||||
}
|
||||
@@ -511,7 +513,7 @@ struct ContentView: View {
|
||||
switch phase {
|
||||
case .background:
|
||||
print("txn: 📙 DAMUS BACKGROUNDED")
|
||||
Task { @MainActor in
|
||||
damusClosingTask = Task { @MainActor in
|
||||
await damus_state.nostrNetwork.close() // Close ndb streaming tasks before closing ndb to avoid memory errors
|
||||
damus_state.ndb.close()
|
||||
}
|
||||
@@ -521,8 +523,11 @@ struct ContentView: View {
|
||||
break
|
||||
case .active:
|
||||
print("txn: 📙 DAMUS ACTIVE")
|
||||
Task {
|
||||
await damusClosingTask?.value // Wait for the closing task to finish before reopening things, to avoid race conditions
|
||||
damus_state.nostrNetwork.connect()
|
||||
damus_state.nostrNetwork.ping()
|
||||
}
|
||||
@unknown default:
|
||||
break
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user