花了一些时间创建@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属性时,都会调用打印功能。如果在0.4秒内多次更新该值,则该函数将仅被调用一次。

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

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

最佳答案

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

嗯...但是如何使用呢?实际上,它不是非常灵活,尤其是在编译器声明“不支持多个属性包装器”之前:-)

如果您的目标是在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()
    }
}

您仍然可以根据需要多次订阅Publisher的model.$debouncedText。而且,如果您希望使用自己的动作来执行操作,也没问题!
model.$debouncedText
    .sink { (s) in
        doSomethingWithDebouncedValue(s)
    }

示例应用程序用法

ios - 防弹 wrapper-LMLPHP

更新:,如果您不能使用Combine,但是您喜欢类似的语法...
首先定义protokol
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)
}

和默认实现的反跳功能。这个想法是,与在Combine上使用.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

关于ios - 防弹 wrapper ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59896772/

10-11 14:06