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

[Relay] Add invalidation of background task #458

Merged
merged 2 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class AutomaticSocketConnectionHandler: SocketConnectionHandler {
}

appStateObserver.onWillEnterForeground = { [unowned self] in
backgroundTaskRegistrar.invalidate()
socket.connect()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import UIKit

protocol BackgroundTaskRegistering {
func register(name: String, completion: @escaping () -> Void)
func invalidate()
}

class BackgroundTaskRegistrar: BackgroundTaskRegistering {
Expand All @@ -20,6 +21,15 @@ class BackgroundTaskRegistrar: BackgroundTaskRegistering {
backgroundTaskID = .invalid
completion()
}
#endif
}

func invalidate() {
#if os(iOS)
if backgroundTaskID != .invalid {
Copy link
Contributor

Choose a reason for hiding this comment

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

why this check required?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

os check because we are using UIApplication

Copy link
Contributor Author

Choose a reason for hiding this comment

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

backgroundTaskID != .invalid to not call endBackgroundTask with invalid task id

UIApplication.shared.endBackgroundTask(backgroundTaskID)
backgroundTaskID = .invalid
}
#endif
}
}
5 changes: 5 additions & 0 deletions Tests/RelayerTests/Mocks/BackgroundTaskRegistrarMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import Foundation

class BackgroundTaskRegistrarMock: BackgroundTaskRegistering {
var completion: (() -> Void)?

func register(name: String, completion: @escaping () -> Void) {
self.completion = completion
}

func invalidate() {

}
}