Skip to content

Commit

Permalink
Merge pull request #467 from xmtp/noe/ios-db-disconnect
Browse files Browse the repository at this point in the history
Drop / reconnect database connections when app becomes backgrounded/active
  • Loading branch information
cameronvoell authored Aug 9, 2024
2 parents bf90e25 + beea3e1 commit daea446
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,36 @@ public class XMTPModule: Module {
actor ClientsManager {
private var clients: [String: XMTP.Client] = [:]

// A method to update the conversations
// A method to update the client
func updateClient(key: String, client: XMTP.Client) {
ContentJson.initCodecs(client: client)
clients[key] = client
}

// A method to retrieve a conversation
// A method to retrieve a client
func getClient(key: String) -> XMTP.Client? {
return clients[key]
}

// A method to disconnect all dbs
func dropAllLocalDatabaseConnections() throws {
for (_, client) in clients {
// Call the drop method on each v3 client
if (!client.installationID.isEmpty) {
try client.dropLocalDatabaseConnection()
}
}
}

// A method to reconnect all dbs
func reconnectAllLocalDatabaseConnections() async throws {
for (_, client) in clients {
// Call the reconnect method on each v3 client
if (!client.installationID.isEmpty) {
try await client.reconnectLocalDatabase()
}
}
}
}

enum Error: Swift.Error {
Expand Down Expand Up @@ -1475,6 +1495,19 @@ public class XMTPModule: Module {

return logOutput
}

OnAppBecomesActive {
Task {
try await clientsManager.reconnectAllLocalDatabaseConnections()
}
}


OnAppEntersBackground {
Task {
try await clientsManager.dropAllLocalDatabaseConnections()
}
}
}

//
Expand Down

0 comments on commit daea446

Please sign in to comment.