Replies: 3 comments 10 replies
-
Most likely you need to combine the debounce with a background assertion to ensure the debounce's underlying timer is allowed to execute in the background. I haven't tested it but it's a pretty typical failure for background execution. This may also just be the case for any async execution, as debounce makes the synchronous notification async. |
Beta Was this translation helpful? Give feedback.
-
Hi @ylorn, I suppose you could also listen for background notifications and save again there: NotificationCenter.default
.publisher(for: UIApplication.didEnterBackgroundNotification)
.sink { standups in
do {
try JSONEncoder().encode(standups).write(to: .standups)
} catch {
}
}
.store(in: &self.cancellables) You can also write a test for this by posting the |
Beta Was this translation helpful? Give feedback.
-
I guess we have to live with a cumbersome solution by introduce another publisher to combine with, where we can debounce if the app is in foreground and skip the debouncing if the app is in background: import Combine
import SwiftUI
class ContentViewModel {
private var cancellables: Set<AnyCancellable> = []
@Published private var isBackgrounded = false
@Published var standups: IdentifiedArrayOf<Standup> = []
init() {
NotificationCenter.default
.publisher(for: UIApplication.didBecomeActiveNotification)
.map { _ in false }
.assign(to: &self.$isBackgrounded)
NotificationCenter.default
.publisher(for: UIApplication.didEnterBackgroundNotification)
.map { _ in true }
.assign(to: &self.$isBackgrounded)
self.$standups
.combineLatest(self.$isBackgrounded)
.filter { $0.1 == false }
.debounce(for: .seconds(1), scheduler: DispatchQueue.main)
.map(\.0)
.sink { standups in
print("debounced")
do {
try JSONEncoder().encode(standups).write(to: .standups)
} catch {
}
}
.store(in: &self.cancellables)
self.$standups
.combineLatest(self.$isBackgrounded)
.filter(\.1)
.map(\.0)
.sink { standups in
print("no debounce")
do {
try JSONEncoder().encode(standups).write(to: .standups)
} catch {
}
}
.store(in: &self.cancellables)
}
} With the additional indicator of
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Just finished watching the latest episode of Modern SwiftUI: Effects, Part 2.
In the last section of this episode, an optimization of the persistence is made to avoid unnecessary accessing to the
standups.json
file by using adebounce
Combine operator:But from my experience, if the
self.$standups
publishes an update when the App is in background, then thedebounce
operator simply omits the update, which means thestandups.json
never gets updated with that update.Demo Code:
With code above, when foregrounding the app, the console prints
when backgrounding the app, the console prints
However, if we add the
debounce
logic to the 2 subscriptions:With the debounce logic, when foregrounding the app, the console still prints
However, when backgrounding the app, this time, nothing gets printed, where it should print
backgrounded
just like before the change.How do you cover this case to keep the persistence?
Beta Was this translation helpful? Give feedback.
All reactions