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

fix: Use appropriate background task API for app extensions #138

Merged
merged 1 commit into from
Mar 19, 2024
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
30 changes: 24 additions & 6 deletions Sources/Amplitude/Plugins/Vendors/AppUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,31 @@ import Foundation
}

override func beginBackgroundTask() -> BackgroundTaskCompletionCallback? {
var id: UIBackgroundTaskIdentifier = .invalid
let callback = { () in
UIApplication.shared.endBackgroundTask(id)
id = .invalid
if isRunningInAppExtension {
let semaphore = DispatchSemaphore(value: 0)
ProcessInfo.processInfo.performExpiringActivity(withReason: "Amplitude") { expired in
guard !expired else {
// If we've expired, just let the system terminate the process
return
}
semaphore.wait()
}
return {
semaphore.signal()
}
} else {
var id: UIBackgroundTaskIdentifier = .invalid
let callback = { () in
UIApplication.shared.endBackgroundTask(id)
id = .invalid
}
id = UIApplication.shared.beginBackgroundTask(withName: "amplitude", expirationHandler: callback)
return callback
}
id = UIApplication.shared.beginBackgroundTask(withName: "amplitude", expirationHandler: callback)
return callback
}

private var isRunningInAppExtension: Bool {
return Bundle.main.bundlePath.hasSuffix(".appex")
}
}
#endif
Expand Down
Loading