-
Notifications
You must be signed in to change notification settings - Fork 24
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
Bug: Stream disconnects on App Backgrounding #185
Comments
It seems suspicious that this is only happening on backgrounding and then reforegrounding the app. Were you able to reproduce this @fabriguespe? I think until we can move the swift SDK over to the new You can kind of see how we do setting up a task and canceling it here https://github.com/xmtp/xmtp-react-native/blob/main/ios/XMTPModule.swift#L638-L655 but the cancel would also happen on background and network disconnection etc.. |
After running some tests, the Here is a proposed solution that involves listening for specific app state notifications and managing the stream accordingly, preventing it from 'dying' silently without errors. // Rest of the code ...
func startStream() {
streamTask = Task {
do {
for try await message in conversation.streamMessages() {
let content: String = try message.content()
print("Received message: \(content)")
await MainActor.run {
messages.append(message)
}
}
} catch {
print("Error in message stream: \(error)")
}
}
}
// Rest of the code ...
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { in
streamTask?.cancel()
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { in
Task{
await loadMessages()
startStreamTask()
}
} We cancel the |
Issue:
Streams intermittently stop receiving messages after the app is backgrounded and re-foregrounded.
Reproduce:
Expected:
Stream maintains connection or reconnects after foregrounding.
Actual:
Stream 'dies' silently without errors.
The text was updated successfully, but these errors were encountered: