Skip to content

Commit

Permalink
Drop / reconnect database connections when app becomes backgrounded /…
Browse files Browse the repository at this point in the history
… active
  • Loading branch information
nmalzieu committed Aug 9, 2024
1 parent afef58b commit 3280a24
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions ios/XMTPModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,32 @@ 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 method on each client
try client.dropLocalDatabaseConnection()
}
}

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

enum Error: Swift.Error {
Expand Down Expand Up @@ -1459,6 +1475,18 @@ public class XMTPModule: Module {
}
return await client.contacts.isGroupDenied(groupId: groupId)
}

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

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

//
Expand Down

0 comments on commit 3280a24

Please sign in to comment.