-
Notifications
You must be signed in to change notification settings - Fork 121
[Local Catalog] Persist catalog downloads in the background #16342
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
Merged
joshheald
merged 11 commits into
trunk
from
feat/WOOMOB-1173-parse-catalog-downloads-in-the-background
Nov 14, 2025
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fabb1de
Persist background downloads in the background
joshheald a93899e
Tests for background catalog parsing
joshheald be0cf24
Stop spinner when foregrounding POS
joshheald 5046bfd
Remove unused code
joshheald f350297
Merge branch 'feat/WOOMOB-1173-background-catalog-download-updated' iβ¦
joshheald db3f9d4
Improve test timing/blocking approach
joshheald 5d29062
Fix placement of public function
joshheald 06ae494
Remove unnecessary cleanup
joshheald bc5c1bf
Inject a new UserDefault instance in the unit tests
joshheald fa75864
Improve state observation for refresh control
joshheald d0558a2
Periphery
joshheald File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
Modules/Sources/Networking/Network/BackgroundCatalogDownloadCoordinator.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import Foundation | ||
| import CocoaLumberjackSwift | ||
|
|
||
| /// Coordinates background catalog downloads, including handling app wake events. | ||
| public class BackgroundCatalogDownloadCoordinator { | ||
| private let backgroundDownloader: BackgroundDownloadProtocol | ||
| private let fileManager: FileManager | ||
|
|
||
| public init(backgroundDownloader: BackgroundDownloadProtocol = BackgroundDownloadService(), | ||
| fileManager: FileManager = .default) { | ||
| self.backgroundDownloader = backgroundDownloader | ||
| self.fileManager = fileManager | ||
| } | ||
|
|
||
| /// Handles a background URLSession wake event. | ||
| /// Called from AppDelegate when iOS wakes the app for a completed download. | ||
| /// - Parameters: | ||
| /// - sessionIdentifier: The session identifier from the callback | ||
| /// - completionHandler: Completion handler to call when processing is done | ||
| /// - parseHandler: Closure to parse and persist the downloaded file | ||
| public func handleBackgroundSessionEvent( | ||
| sessionIdentifier: String, | ||
| completionHandler: @escaping () -> Void, | ||
| parseHandler: @escaping (URL, Int64) async throws -> Void | ||
| ) async { | ||
| DDLogInfo("π£ Handling background session event for: \(sessionIdentifier)") | ||
|
|
||
| // Load the saved download state to know which site this is for | ||
| guard let state = BackgroundDownloadState.load(for: sessionIdentifier) else { | ||
| DDLogError("βοΈ No saved state found for background download session: \(sessionIdentifier)") | ||
| completionHandler() | ||
| return | ||
| } | ||
|
|
||
| // Reconnect to the background session and get the downloaded file | ||
| guard let fileURL = await backgroundDownloader.reconnectToSession(identifier: sessionIdentifier, | ||
| allowCellular: true, | ||
| completionHandler: completionHandler) else { | ||
| DDLogError("βοΈ Failed to reconnect to background download session") | ||
| BackgroundDownloadState.clear() | ||
| return | ||
| } | ||
|
|
||
| DDLogInfo("π£ Background download file ready at: \(fileURL.path)") | ||
|
|
||
| // Parse the catalog file in this background window (~30 seconds) | ||
| // TODO: WOOMOB-1677 - For very large catalogs, consider hybrid approach: try immediate parse, defer if timeout. | ||
| do { | ||
| try await parseHandler(fileURL, state.siteID) | ||
| DDLogInfo("β Background catalog processing completed successfully") | ||
| } catch { | ||
| DDLogError("βοΈ Failed to process catalog in background: \(error)") | ||
| } | ||
|
|
||
| // Clean up state | ||
| BackgroundDownloadState.clear() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
Modules/Sources/Networking/Network/BackgroundDownloadState.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import Foundation | ||
|
|
||
| /// Persisted state for background catalog downloads. | ||
| /// Allows the app to resume processing downloads after being terminated. | ||
| public struct BackgroundDownloadState: Codable { | ||
| let sessionIdentifier: String | ||
| let siteID: Int64 | ||
| let startedAt: Date | ||
|
|
||
| private static let userDefaultsKey = "com.woocommerce.pos.backgroundDownloadState" | ||
|
|
||
| /// Saves download state for later retrieval. | ||
| public static func save(_ state: BackgroundDownloadState) { | ||
| let encoder = JSONEncoder() | ||
| if let encoded = try? encoder.encode(state) { | ||
| UserDefaults.standard.set(encoded, forKey: userDefaultsKey) | ||
| } | ||
| } | ||
|
|
||
| /// Loads saved download state for a specific session identifier. | ||
| public static func load(for sessionIdentifier: String) -> BackgroundDownloadState? { | ||
| guard let data = UserDefaults.standard.data(forKey: userDefaultsKey), | ||
| let state = try? JSONDecoder().decode(BackgroundDownloadState.self, from: data), | ||
| state.sessionIdentifier == sessionIdentifier else { | ||
| return nil | ||
| } | ||
| return state | ||
| } | ||
|
|
||
| /// Clears saved download state. | ||
| public static func clear() { | ||
| UserDefaults.standard.removeObject(forKey: userDefaultsKey) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: We can move this signature to a different extension or to the class. I get a warning here:
'public' modifier conflicts with extension's default access of 'private'