Don't start a second sync while syncing is already in progress

This commit is contained in:
Shadowfacts 2022-01-26 23:13:20 -05:00
parent 9f9a214b0a
commit aebd3a910d
2 changed files with 9 additions and 5 deletions

View File

@ -26,7 +26,7 @@ class FervorController {
private(set) var persistentContainer: PersistentContainer!
let syncStateSubject = PassthroughSubject<SyncState, Never>()
@Published private(set) var syncState = SyncState.done
init(instanceURL: URL) {
self.instanceURL = instanceURL
@ -47,7 +47,7 @@ class FervorController {
private func setSyncState(_ state: SyncState) {
DispatchQueue.main.async {
self.syncStateSubject.send(state)
self.syncState = state
}
}
@ -65,6 +65,12 @@ class FervorController {
}
func syncAll() async throws {
guard syncState == .done else {
return
}
// always return to .done, even if we throw and stop syncing early
defer { setSyncState(.done) }
setSyncState(.groupsAndFeeds)
logger.info("Syncing groups and feeds")
@ -82,8 +88,6 @@ class FervorController {
setSyncState(.excerpts)
await ExcerptGenerator.generateAll(self)
setSyncState(.done)
}
@MainActor

View File

@ -93,7 +93,7 @@ class HomeViewController: UIViewController {
feedResultsController.delegate = self
try! feedResultsController.performFetch()
fervorController.syncStateSubject
fervorController.$syncState
.debounce(for: .milliseconds(250), scheduler: RunLoop.main, options: nil)
.sink { [unowned self] in
self.syncStateChanged($0)