Skip to content
Closed
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 @@ -254,30 +254,31 @@ class CrowdinSupportedLanguages {

private func notifySuccess(_ languages: [CrowdinLanguage]?) {
let manifestTimestamp = pendingManifestTimestamp

// Extract data to save and callbacks in a single sync block

// On a 304 Not Modified response, check outside queue.sync whether we need to load from disk,
// then read from disk outside the queue to avoid blocking it with file I/O.
let needsDiskLoad: Bool = languages == nil && queue.sync { _supportedLanguages == nil }
let diskLanguages: [CrowdinLanguage]? = needsDiskLoad ? loadLanguagesFromDisk() : nil

// Update in-memory state and collect callbacks in a single sync block
let (languagesToSave, callbacks): ([CrowdinLanguage]?, [() -> Void]) = queue.sync {
if let languages = languages {
self._supportedLanguages = languages
} else if self._supportedLanguages == nil {
// On 304 (Not Modified), load from cache if we don't have it in memory yet
let cachedData = try? Data(contentsOf: URL(fileURLWithPath: self.filePath))
if let data = cachedData {
self._supportedLanguages = try? JSONDecoder().decode([DistributionLanguage].self, from: data)
}
} else if let diskLanguages = diskLanguages {
self._supportedLanguages = diskLanguages
}
let completions = self._completions
self._errors.removeAll()
self._completions.removeAll()
self._loading = false
self.pendingManifestTimestamp = nil

// Return both languages and callbacks
return (self._supportedLanguages, completions)
}

// Save to disk outside of the queue to avoid file I/O blocking the queue
saveSupportedLanguages(languages: languagesToSave)

// Only persist to disk when data arrived from the network (not re-written from disk on 304).
if languages != nil {
saveSupportedLanguages(languages: languagesToSave)
}

if let manifestTimestamp = manifestTimestamp {
fileTimestampStorage.updateTimestamp(for: TimestampKeys.localization, filePath: TimestampKeys.filePath, timestamp: manifestTimestamp)
Expand All @@ -290,6 +291,11 @@ class CrowdinSupportedLanguages {
}
}

private func loadLanguagesFromDisk() -> [CrowdinLanguage]? {
guard let data = try? Data(contentsOf: URL(fileURLWithPath: filePath)) else { return nil }
return try? JSONDecoder().decode([DistributionLanguage].self, from: data)
}

fileprivate func saveSupportedLanguages(languages: [CrowdinLanguage]?) {
guard let crowdlanguages = languages else { return }

Expand Down