2

在花了一些时间创建 @Debounced 属性包装器之后,我对代码的可读性不满意。要了解发生了什么,您确实需要了解属性包装器的工作原理以及包装值和投影值的概念。这是属性包装器:

    @propertyWrapper
    class Debounced<Input: Hashable> {

    private var delay: Double
    private var _value: Input
    private var function: ((Input) -> Void)?
    private weak var timer: Timer?

    public init(wrappedValue: Input, delay: Double) {
        self.delay = delay
        self._value = wrappedValue
    }

    public var wrappedValue: Input {
        get {
            return _value
        }
        set(newValue) {
            timer?.invalidate()
            timer = Timer.scheduledTimer(withTimeInterval: delay, repeats: false, block: { [weak self] _ in
                self?._value = newValue
                self?.timer?.invalidate()
                self?.function?(newValue)
            })
        }
    }

    public var projectedValue: ((Input) -> Void)? {
        get {
            return function
        }
        set(newValue) {
            function = newValue
        }
    }
}

属性包装器的使用方式如下:

@Debounced(delay: 0.4) var text: String? = nil

override func viewDidLoad() {
    super.viewDidLoad()

    self.$text = { text in
        print(text)
    }
}

它可以正常工作。每次设置 text 属性时,都会调用 print 函数。如果值在 0.4 秒内多次更新,则该函数只会被调用一次。

但就简单性和可读性而言,我认为最好像这样创建一个 Debouncer 类:https ://github.com/webadnan/swift-debouncer 。

你怎么看?有没有更好的方法来创建这个属性包装器?

4

1 回答 1

3

它可以正常工作......在这种情况下,只需使用它!

嗯……但是怎么用呢?实际上,它不是很灵活,尤其是在编译器声称“不支持多个属性包装器”之前:-)

如果您的目标是在 UIKit 或 SwiftUI 应用程序中使用它,我建议您采用不同的方法。

让我们尝试一些简约但功能齐全的 SwiftUI 示例

//
//  ContentView.swift
//  tmp031
//
//  Created by Ivo Vacek on 26/01/2020.
//  Copyright © 2020 Ivo Vacek. NO rights reserved.
//

import SwiftUI
import Combine

class S: ObservableObject {
    @Published var text: String = ""
    @Published var debouncedText: String = ""

    private var store = Set<AnyCancellable>()
    init(delay: Double) {
        $text
            .debounce(for: .seconds(delay), scheduler: RunLoop.main)
            .sink { [weak self] (s) in
            self?.debouncedText = s
        }.store(in: &store)
    }
}

struct ContentView: View {
    @ObservedObject var model = S(delay: 2)
    var body: some View {
        List {
            Color.clear
            Section(header: Text("Direct")) {
                Text(model.text).font(.title)
            }
            Section(header: Text("Debounced")) {
                Text(model.debouncedText).font(.title)
            }
            Section(header: Text("Source")) {
                TextField("type here", text: $model.text).font(.title)
            }

        }
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

model.$debouncedText您仍然可以根据需要多次订阅Publisher。如果你喜欢用你自己的动作来执行,也没问题!

model.$debouncedText
    .sink { (s) in
        doSomethingWithDebouncedValue(s)
    }

示例应用程序用法

在此处输入图像描述

更新:如果你不能使用组合,但你喜欢类似的语法......首先定义协议

protocol Debounce: class {
    associatedtype Value: Hashable
    var _value: Value { get set }
    var _completions: [(Value)->Void] { get set}
    var _delay: TimeInterval { get set }
    var _dw: DispatchWorkItem! { get set }
    func debounce(completion: @escaping (Value)->Void)
}

和去抖动功能的默认实现。这个想法是,以相同的方式使用 debounce,就像组合上的 .publisher.sink() 一样。_debounce 是去抖动功能的“内部”实现。它比较当前值和“延迟”旧值,如果它们相等,就完成这项工作。

extension Debounce {
    func debounce(completion: @escaping (Value)->Void) {
        _completions.append(completion)
    }
    func _debounce(newValue: Value, delay: TimeInterval, completions:  [(Value)->Void]) {
        if _dw != nil {
            _dw.cancel()
        }
        var dw: DispatchWorkItem!
        dw = DispatchWorkItem(block: { [weak self, newValue, completions] in
            if let s = self, s._value == newValue {
                for completion in completions {
                    completion(s._value)
                }
            }
            dw = nil
        })
        _dw = dw
        DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: dw)
    }
}

现在我们有了属性包装器的所有组件。

@propertyWrapper class Debounced<T: Hashable> {

    final class Debouncer: Debounce {
        typealias Value = T

        var _completions: [(T) -> Void] = []
        var _delay: TimeInterval
        var _value: T {
            willSet {
                _debounce(newValue: newValue, delay: _delay, completions: _completions)
            }
        }
        var _dw: DispatchWorkItem!
        init(_value: T, _delay: TimeInterval) {
            self._value = _value
            self._delay = _delay
        }
    }

    var wrappedValue: T {
        get { projectedValue._value }
        set { projectedValue._value = newValue }
    }

    var projectedValue: Debouncer

    init(wrappedValue: T, delay: TimeInterval) {
        projectedValue = Debouncer(_value: wrappedValue, _delay: delay)
    }
    deinit {
        print("deinit")
    }
}

让我们试试

do {
    struct S {
        @Debounced(delay: 0.2) var value: Int = 0
    }

    let s = S()
    print(Date(), s.value, "initial")

    s.$value.debounce { (i) in
        print(Date(), i, "debounced A")
    }

    s.$value.debounce { (i) in
        print(Date(), i, "debounced B")
    }

    var t = 0.0
    (0 ... 8).forEach { (i) in
        let dt = Double.random(in: 0.0 ... 0.6)
        t += dt
        DispatchQueue.main.asyncAfter(deadline: .now() + t) { [t] in
            s.value = i
            print(s.value, t)
        }
    }
}

打印类似的东西

2020-02-04 09:53:11 +0000 0 initial
0 0.46608517831539165
2020-02-04 09:53:12 +0000 0 debounced A
2020-02-04 09:53:12 +0000 0 debounced B
1 0.97078412234771
2 1.1756938500918692
3 1.236562020385944
4 1.4076127046937024
2020-02-04 09:53:13 +0000 4 debounced A
2020-02-04 09:53:13 +0000 4 debounced B
5 1.9313412744029004
6 2.1617775513150366
2020-02-04 09:53:14 +0000 6 debounced A
2020-02-04 09:53:14 +0000 6 debounced B
7 2.6665465865810205
8 2.9287734023206418
deinit
于 2020-02-01T19:46:46.643 回答