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

open send push registration request #118

Merged
merged 1 commit into from
Feb 18, 2025
Merged
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
29 changes: 20 additions & 9 deletions Sources/UBPush/UBPushRegistrationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,29 @@ open class UBPushRegistrationManager: NSObject {
sendPushRegistration(completion: completion)
}

/// :nodoc:
private func sendPushRegistration(completion: (@Sendable (Error?) -> Void)? = nil) {
open func sendPushRegistrationRequest(completion: (@escaping @Sendable (Result<String, Error>) -> Void)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Muss das open sein oder reicht auch public? Resp. wollen wir das jemals überschreiben (bei Post rufen wir diese Methode einfach separat auf, oder?).
Falls open, müssten wir evt. noch eine Funktion machen, die den backgroundTask beenden kann, damit wirs auch von aussen machen können und nicht vergessen geht.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

die Idee war, dass man sendPushRegistrationRequest überschreiben kann und damit die logik, wie requests abgesetzt werden. sendPushRegistration hingegen ist private, damit man die logik vom background task nicht ändern kann.

guard let registrationRequest = pushRegistrationRequest else {
completion?(UBPushManagerError.registrationRequestMissing)
completion(.failure(UBPushManagerError.registrationRequestMissing))
return
}

task = UBURLDataTask(request: registrationRequest, session: session)
task?.addCompletionHandler(decoder: UBHTTPStringDecoder()) { result, _, _, _ in
switch result {
case .success(let value): completion(.success(value))
case .failure(let error): completion(.failure(error))
}
}

if task != nil {
modifyRegistrationDataTask(&task!)
}

task?.start()
}
Comment on lines +95 to +100
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Avoid force unwrapping task!.
Even with the preceding check if task != nil, it’s safer to use optional binding to prevent accidental crashes if someone refactors the code later.

-if task != nil {
-    modifyRegistrationDataTask(&task!)
-}
+if var dataTask = task {
+    modifyRegistrationDataTask(&dataTask)
+}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if task != nil {
modifyRegistrationDataTask(&task!)
}
task?.start()
}
if var dataTask = task {
modifyRegistrationDataTask(&dataTask)
}
task?.start()
}
🧰 Tools
🪛 SwiftLint (0.57.0)

[Warning] 96-96: Force unwrapping should be avoided

(force_unwrapping)


/// :nodoc:
private func sendPushRegistration(completion: (@Sendable (Error?) -> Void)? = nil) {
if backgroundTask == .invalid {
backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
guard let self else {
Expand All @@ -96,8 +112,7 @@ open class UBPushRegistrationManager: NSObject {
}
}

task = UBURLDataTask(request: registrationRequest, session: session)
task?.addCompletionHandler(decoder: UBHTTPStringDecoder()) { [weak self] result, _, _, _ in
sendPushRegistrationRequest { [weak self] result in
MainActor.assumeIsolated {
guard let self else {
return
Expand All @@ -122,11 +137,7 @@ open class UBPushRegistrationManager: NSObject {
}
}

if task != nil {
modifyRegistrationDataTask(&task!)
}

task?.start()
UBPushManager.logger.info("\(String(describing: self)) started")
}

Expand Down
Loading