如何创建日期调度程序?
How create date schedular?
我正在尝试创建一个日期调度程序来观察某些事件。但它不起作用。我浏览了
protocol DateScheduler
并且据说 action 在某些方法中 currentDate 会发生 protocol DateScheduler.我正在尝试在 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()
}
}
我正在 AppDelegate 中进行。 ObserverSchedular 和 signalSchedular 是全局属性。请向我解释如何调用所有方法 DateScheduler.
我已经明白我错在哪里了。为了修复它,我接下来做了:
- 已创建自定义
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)
}
}
}
}
}
}
已在 didFinishLaunchingWithOptions
中创建观察绑定
reactive.makeBindingTarget { appDeleg, value in
print("SUCCESS")
print(value)
} <~ signalSchedular.customObserve(on: schedular, interval: 10, leeway: 1)
并模拟异步代码
DispatchQueue.main.async { [observerSchedular] in
observerSchedular.send(value: "Hello World")
observerSchedular.sendCompleted()
}
- 定义的全局属性
private let (signalSchedular, observerSchedular) = Signal<String, Never>.pipe(),
let schedular = SomeDateScheduler()
- 更改了自定义日期安排
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
}
}
我正在尝试创建一个日期调度程序来观察某些事件。但它不起作用。我浏览了
protocol DateScheduler
并且据说 action 在某些方法中 currentDate 会发生 protocol DateScheduler.我正在尝试在 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()
}
}
我正在 AppDelegate 中进行。 ObserverSchedular 和 signalSchedular 是全局属性。请向我解释如何调用所有方法 DateScheduler.
我已经明白我错在哪里了。为了修复它,我接下来做了:
- 已创建自定义
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)
}
}
}
}
}
}
已在
中创建观察绑定didFinishLaunchingWithOptions
reactive.makeBindingTarget { appDeleg, value in print("SUCCESS") print(value) } <~ signalSchedular.customObserve(on: schedular, interval: 10, leeway: 1)
并模拟异步代码
DispatchQueue.main.async { [observerSchedular] in
observerSchedular.send(value: "Hello World")
observerSchedular.sendCompleted()
}
- 定义的全局属性
private let (signalSchedular, observerSchedular) = Signal<String, Never>.pipe(),
let schedular = SomeDateScheduler()
- 更改了自定义日期安排
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
}
}