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 {
self?.latestKnownAppData = AppData(selectedIndex: selectedIndex, locations: locations)
}
saveDelayQueue.cancelAllOperations()
let saveOperation = BlockOperation()
saveOperation.addExecutionBlock { [weak saveOperation, weak self] in
guard saveOperation?.isCancelled == false else {
let saveWithDelayOperation = BlockOperation()
saveWithDelayOperation.addExecutionBlock { [weak saveWithDelayOperation, weak self] in
guard saveWithDelayOperation?.isCancelled == false else {
return
}
if let delay = self?.delay, delay > 0 {
Thread.sleep(forTimeInterval: delay)
}
guard saveOperation?.isCancelled == false else {
guard saveWithDelayOperation?.isCancelled == false else {
return
}
self?.saveQueue.addOperation {
if saveOperation?.isCancelled != true {
let saveOperation = BlockOperation()
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?.latestKnownAppData = nil
}
}
}
saveDelayQueue.addOperation(saveOperation)
saveDelayQueue.addOperation(saveWithDelayOperation)
}
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