From 3280a24461544332883ebbda25aba7649bd54a4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noe=CC=81=20Malzieu?= Date: Fri, 9 Aug 2024 10:49:32 +0200 Subject: [PATCH] Drop / reconnect database connections when app becomes backgrounded / active --- ios/XMTPModule.swift | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/ios/XMTPModule.swift b/ios/XMTPModule.swift index 06feda5d4..965c31817 100644 --- a/ios/XMTPModule.swift +++ b/ios/XMTPModule.swift @@ -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 { @@ -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() + } + } } //