From 3da8fc1be179d77cc80855429e72b8de92519288 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 1/7] 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 39d3a16d5..10620ece3 100644 --- a/ios/XMTPModule.swift +++ b/ios/XMTPModule.swift @@ -48,16 +48,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 { @@ -1475,6 +1491,18 @@ public class XMTPModule: Module { return logOutput } + + OnAppBecomesActive { + Task { + try await clientsManager.reconnectAllLocalDatabaseConnections() + } + } + + OnAppEntersBackground { + Task { + try await clientsManager.dropAllLocalDatabaseConnections() + } + } } // From 58d57ba1fd4e9e0d4b93d2adb4e6938630592533 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noe=CC=81=20Malzieu?= Date: Fri, 9 Aug 2024 10:55:00 +0200 Subject: [PATCH 2/7] Fix spacing --- ios/XMTPModule.swift | 57 +++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/ios/XMTPModule.swift b/ios/XMTPModule.swift index 10620ece3..d8608f5a8 100644 --- a/ios/XMTPModule.swift +++ b/ios/XMTPModule.swift @@ -60,20 +60,20 @@ public class XMTPModule: Module { } // 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() - } - } + 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 { @@ -1476,6 +1476,12 @@ public class XMTPModule: Module { } return await client.contacts.isGroupDenied(groupId: groupId) } + + OnAppBecomesActive { + Task { + try await clientsManager.reconnectAllLocalDatabaseConnections() + } + } AsyncFunction("exportNativeLogs") { () -> String in var logOutput = "" @@ -1492,17 +1498,18 @@ public class XMTPModule: Module { return logOutput } - OnAppBecomesActive { - Task { - try await clientsManager.reconnectAllLocalDatabaseConnections() - } - } - - OnAppEntersBackground { - Task { - try await clientsManager.dropAllLocalDatabaseConnections() - } - } + OnAppBecomesActive { + Task { + try await clientsManager.reconnectAllLocalDatabaseConnections() + } + } + + + OnAppEntersBackground { + Task { + try await clientsManager.dropAllLocalDatabaseConnections() + } + } } // From dba247269bc8966c63509e86b4c7f351b03d4217 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noe=CC=81=20Malzieu?= Date: Fri, 9 Aug 2024 10:56:03 +0200 Subject: [PATCH 3/7] Fix spacing --- ios/XMTPModule.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ios/XMTPModule.swift b/ios/XMTPModule.swift index d8608f5a8..31e75cbfa 100644 --- a/ios/XMTPModule.swift +++ b/ios/XMTPModule.swift @@ -59,7 +59,7 @@ public class XMTPModule: Module { return clients[key] } - // A method to disconnect all dbs + // A method to disconnect all dbs func dropAllLocalDatabaseConnections() throws { for (_, client) in clients { // Call the method on each client @@ -67,7 +67,7 @@ public class XMTPModule: Module { } } - // A method to disconnect all dbs + // A method to reconnect all dbs func reconnectAllLocalDatabaseConnections() async throws { for (_, client) in clients { // Call the method on each client From 65af5a95c466fa1a29216b127e4a13e30d2f0773 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noe=CC=81=20Malzieu?= Date: Fri, 9 Aug 2024 17:28:27 +0200 Subject: [PATCH 4/7] feat: enable automated database disconnection on the native thread From dd9fb5dd40a89eb323a04626b9d14d4a2fefa441 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noe=CC=81=20Malzieu?= Date: Fri, 9 Aug 2024 17:49:16 +0200 Subject: [PATCH 5/7] Verify that the client is v3 enabled first --- ios/XMTPModule.swift | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/ios/XMTPModule.swift b/ios/XMTPModule.swift index 31e75cbfa..b859ac946 100644 --- a/ios/XMTPModule.swift +++ b/ios/XMTPModule.swift @@ -62,16 +62,20 @@ public class XMTPModule: Module { // A method to disconnect all dbs func dropAllLocalDatabaseConnections() throws { for (_, client) in clients { - // Call the method on each client - try client.dropLocalDatabaseConnection() + // 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 method on each client - try await client.reconnectLocalDatabase() + // Call the reconnect method on each v3 client + if (!client.installationID.isEmpty) { + try await client.reconnectLocalDatabase() + } } } } From 950976aa209cdf783b55f469588322989ced02b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noe=CC=81=20Malzieu?= Date: Fri, 9 Aug 2024 17:50:15 +0200 Subject: [PATCH 6/7] spacing --- ios/XMTPModule.swift | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ios/XMTPModule.swift b/ios/XMTPModule.swift index b859ac946..2d478c4c0 100644 --- a/ios/XMTPModule.swift +++ b/ios/XMTPModule.swift @@ -63,19 +63,19 @@ public class XMTPModule: Module { func dropAllLocalDatabaseConnections() throws { for (_, client) in clients { // Call the drop method on each v3 client - if (!client.installationID.isEmpty) { - try client.dropLocalDatabaseConnection() - } + 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() - } + // Call the reconnect method on each v3 client + if (!client.installationID.isEmpty) { + try await client.reconnectLocalDatabase() + } } } } From beea3e17bed73a096c0e9a63b5c6d45ce8da58f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Noe=CC=81=20Malzieu?= Date: Fri, 9 Aug 2024 17:58:19 +0200 Subject: [PATCH 7/7] Remove duplicated code --- ios/XMTPModule.swift | 6 ------ 1 file changed, 6 deletions(-) diff --git a/ios/XMTPModule.swift b/ios/XMTPModule.swift index 2d478c4c0..788b0fa9c 100644 --- a/ios/XMTPModule.swift +++ b/ios/XMTPModule.swift @@ -1480,12 +1480,6 @@ public class XMTPModule: Module { } return await client.contacts.isGroupDenied(groupId: groupId) } - - OnAppBecomesActive { - Task { - try await clientsManager.reconnectAllLocalDatabaseConnections() - } - } AsyncFunction("exportNativeLogs") { () -> String in var logOutput = ""