Commit 2686098f by Demid Merzlyakov

DelayedSaveStorage: only use cached data while saving is in progress.

parent 3696209f
...@@ -54,24 +54,29 @@ public class DelayedSaveStorage: Storage { ...@@ -54,24 +54,29 @@ public class DelayedSaveStorage: Storage {
self?.latestKnownAppData = AppData(selectedIndex: selectedIndex, locations: locations) self?.latestKnownAppData = AppData(selectedIndex: selectedIndex, locations: locations)
} }
saveDelayQueue.cancelAllOperations() saveDelayQueue.cancelAllOperations()
let saveOperation = BlockOperation() let saveWithDelayOperation = BlockOperation()
saveOperation.addExecutionBlock { [weak saveOperation, weak self] in saveWithDelayOperation.addExecutionBlock { [weak saveWithDelayOperation, weak self] in
guard saveOperation?.isCancelled == false else { guard saveWithDelayOperation?.isCancelled == false else {
return return
} }
if let delay = self?.delay, delay > 0 { if let delay = self?.delay, delay > 0 {
Thread.sleep(forTimeInterval: delay) Thread.sleep(forTimeInterval: delay)
} }
guard saveOperation?.isCancelled == false else { guard saveWithDelayOperation?.isCancelled == false else {
return return
} }
self?.saveQueue.addOperation { let saveOperation = BlockOperation()
if saveOperation?.isCancelled != true { saveOperation.addExecutionBlock { [weak saveOperation, weak self] in
guard saveWithDelayOperation?.isCancelled != true && saveOperation?.isCancelled != true else {
return
}
self?.latestKnownAppDataSynchronizationQueue.addOperation {
self?.storage.save(locations: locations, selectedIndex: selectedIndex) self?.storage.save(locations: locations, selectedIndex: selectedIndex)
self?.latestKnownAppData = nil
} }
} }
} }
saveDelayQueue.addOperation(saveOperation) saveDelayQueue.addOperation(saveWithDelayOperation)
} }
public func load(completion: @escaping StorageCompletion) { public func load(completion: @escaping StorageCompletion) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment