Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop / reconnect database connections when app becomes backgrounded/active #467

Merged
merged 7 commits into from
Aug 9, 2024
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 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 @@ -1460,6 +1480,12 @@ public class XMTPModule: Module {
}
return await client.contacts.isGroupDenied(groupId: groupId)
}

OnAppBecomesActive {
Task {
try await clientsManager.reconnectAllLocalDatabaseConnections()
neekolas marked this conversation as resolved.
Show resolved Hide resolved
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a duplicate of line 1504

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry about that must have been the conflict when rebasing. Fixing.


AsyncFunction("exportNativeLogs") { () -> String in
var logOutput = ""
Expand All @@ -1475,6 +1501,19 @@ public class XMTPModule: Module {

return logOutput
}

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


OnAppEntersBackground {
Task {
try await clientsManager.dropAllLocalDatabaseConnections()
}
}
Comment on lines +1499 to +1510
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to be a little more carful about this since these methods will error for any client that does not pass a V3Enabled flag. So they should only be called if the client is v3enabled.

}

//
Expand Down
Loading