Issue #876
Make a dedicate DebounceObject to debounce (or throttle). Then we can even observe with onChange
on the debouncedText
or just use it directly to sort
import Combine
public final class DebounceObject: ObservableObject {
@Published var text: String = ""
@Published var debouncedText: String = ""
private var bag = Set<AnyCancellable>()
public init(dueTime: TimeInterval = 0.5) {
$text
.removeDuplicates()
.debounce(for: .seconds(dueTime), scheduler: DispatchQueue.main)
.sink(receiveValue: { [weak self] value in
self?.debouncedText = value
})
.store(in: &bag)
}
}
struct SearchView: View {
@StateObject var debounceObject = DebounceObject()
var body: some View {
VStack {
TextField(text: $debounceObject.text)
.onChange(of: debounceObject.debouncedText) { text in
// perform search
}
}
}
}