0

我正在尝试创建一个日期调度程序来观察某些事件。但它不起作用。我已经看过了

协议日期调度器

并且据说在某些方法协议 DateScheduler中将currentDate执行操作。我在 10 秒后尝试这样做。下面是我的自定义计划的示例。

class SomeDateScheduler : DateScheduler {
var currentDate: Date
init() {
    self.currentDate = Date(timeIntervalSinceNow: 10)
}

func schedule(after date: Date, action: @escaping () -> Void) -> Disposable? {
    print(#function)
    print(date)
    return nil
}

func schedule(after date: Date, interval: DispatchTimeInterval, leeway: DispatchTimeInterval, action: @escaping () -> Void) -> Disposable? {
    print(#function)
    print(date)
    print(interval)
    print(leeway)
    return nil
}

func schedule(_ action: @escaping () -> Void) -> Disposable? {
    print(#function)
    return nil
}

}

然后我创建绑定来观察事件

private func testSchedular() {
    let schedular = SomeDateScheduler()

    reactive.makeBindingTarget { appDeleg, value in
        print("SUCCESS")
        print(value)
        } <~ signalSchedular.observe(on: schedular)

    DispatchQueue.main.async { [observerSchedular] in
        observerSchedular.send(value: "Hello World")
        observerSchedular.sendCompleted()
    }
}

我正在做AppDelegateObserverSchedularsignalSchedular是全局属性。请向我解释如何调用所有方法DateScheduler

4

1 回答 1

0

我已经明白我在哪里犯了错误。为了解决这个问题,我接下来做了:

  1. 创建自定义observe(on: Schedular). 所以它看起来像这样(将这些函数添加到扩展中Signal):
func customObserve(on scheduler: DateScheduler, interval: Int, leeway: Int) -> Signal<Value, Error> {
    return customFlatMapEvent(Signal.Event.observe(on: scheduler, interval: DispatchTimeInterval.seconds(interval), leeway: DispatchTimeInterval.seconds(leeway)))
}


func customFlatMapEvent<U, E>(_ transform: @escaping Event.CustomTransformation<U, E>) -> Signal<U, E> {
    return Signal<U, E> { output, lifetime in
        let input = transform(output.send, lifetime)
        lifetime += self.observe(input)
    }
}

还添加这个

extension Signal.Event {
    typealias CustomTransformation<U, E: Swift.Error> = (@escaping Signal<U, E>.Observer.Action, Lifetime) -> Signal<Value, Error>.Observer.Action
    
    static func observe(on scheduler: DateScheduler, interval: DispatchTimeInterval, leeway: DispatchTimeInterval) -> CustomTransformation<Value, Error> {
        return { action, lifetime in
            lifetime.observeEnded {
                scheduler.schedule {
                    action(.interrupted)
                }
            }

            return { event in
                scheduler.schedule(after: scheduler.currentDate, interval: interval, leeway: leeway) {
                    if !lifetime.hasEnded {
                        action(event)
                    }
                }
            }
        }
    }
}
  1. 为观察创建绑定didFinishLaunchingWithOptions

    reactive.makeBindingTarget { appDeleg, value in
        print("SUCCESS")
        print(value)
        } <~ signalSchedular.customObserve(on: schedular, interval: 10, leeway: 1)
    
  2. 并模拟异步代码


DispatchQueue.main.async { [observerSchedular] in
       observerSchedular.send(value: "Hello World")
       observerSchedular.sendCompleted()
}

  1. 定义的全局属性
private let (signalSchedular, observerSchedular) = Signal<String, Never>.pipe(),
let schedular = SomeDateScheduler()
  1. 更改了自定义日期计划
class SomeDateScheduler : DateScheduler {
    var currentDate: Date
    init() {
        self.currentDate = Date()
    }
    
    func schedule(after date: Date, action: @escaping () -> Void) -> Disposable? {
        action()
        return nil
    }
    
    func schedule(after date: Date, interval: DispatchTimeInterval, leeway: DispatchTimeInterval, action: @escaping () -> Void) -> Disposable? {
        if case .seconds(let sec) = interval {
            let time = DispatchTime.now() + DispatchTimeInterval.seconds(sec)
            let queue = DispatchQueue(label: "sads", qos: .utility, attributes: .concurrent)
            queue.asyncAfter(deadline: time) { [weak self] in
                _ = self?.schedule(after: date) {
                    action()
                }
            }


            DispatchQueue.main.asyncAfter(deadline: time) { [weak self] in
                _ = self?.schedule(after: date) {
                    action()
                }
            }
        }
        return nil
    }
    
    func schedule(_ action: @escaping () -> Void) -> Disposable? {
        action()
        return nil
    }
}
于 2019-10-28T13:29:36.620 回答