Skip to content

Conversation

@ajinumoto
Copy link

Fix multiple NWConnection during migrateConnection

Fix multiple NWConnection during migrateConnection
@ajinumoto
Copy link
Author

Problem:
Every time migrateConnection() is called, new NWConnection will be created. The problem appear if migrateConnection() is called multiple time, the previous NWConnection from migratedConnection variable is never closed and multiple NWConnection will be alive.

Solution
change migratedConnection to global variable and make sure its cancel() and nil first before creating new one.

private func migrateConnection(completionHandler: @escaping (Result<WebSocketConnection, NWError>) -> Void) {

let migratedConnection = NWConnection(to: endpoint, using: parameters)
migratedConnection?.cancel()

Choose a reason for hiding this comment

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

if migrateConnection was already called and successfully ended, in the next call when you cancelling migratedConnection you cancelling current 'connection'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants