如何安排时间表?

时间:2019-10-27 09:01:09

标签: ios swift reactive-cocoa reactive-swift

我正在尝试创建日期调度程序以观察某些事件。但这行不通。我已经浏览过

  

协议DateScheduler

据说,操作将通过某些方法协议DateScheduler currentDate 发生。我尝试在10秒后执行此操作。以下是我的自定义时间表的示例。

var lifeA = 500    
var lifeB = 250
var powerA = 2
var powerB = 4
var increasepowerA = Math.floor(Math.random() * 5)
var increasepowerB = Math.floor(Math.random() * 10)
var chanceattack = 0.5
var x = 0


function getx(){
  const z = Math.random()
  if (z <= chanceattack){
     x = 1
  }else{x = 0}
}

getx()

function execute(){
  if(x = 1){attackA()
  }else{attackB()}
}


function attackB(){
  lifeA -= powerB;
  powerB += increasepowerB;
  chanceattack += 0.075;
  if (lifeA <= 0){
    console.log("A has dead")
  }else{getx(); execute()}
}

function attackA(){
  lifeB -= powerA;
  powerA += increasepowerA;
  chanceattack -= 0.075;
  if (lifeB <= 0){
    console.log("B has dead")

  }else{getx(); execute()}
}

execute()

}

然后创建绑定以观察事件

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
}

我正在将其导入 AppDelegate ObserverSchedular signalSchedular 是全局属性。请向我解释如何调用所有方法 DateScheduler

1 个答案:

答案 0 :(得分:0)

我已经知道我在哪里犯错了。要解决此问题,我接下来要做:

  1. 已创建自定义observe(on: Schedular)。所以看起来像这样: enter image description here

  2. 已创建绑定以在didFinishLaunchingWithOptions中进行观察

    reactive.makeBindingTarget { appDeleg, value in
        print("SUCCESS")
        print(value)
        } <~ signalSchedular.customObserve(on: schedular, interval: 10, leeway: 1)
    
  3. 并模拟异步代码 enter image description here
  4. 定义的全局属性 private let (signalSchedular, observerSchedular) = Signal<String, Never>.pipe(), let schedular = SomeDateScheduler()
  5. 更改了自定义日期的时间表 enter image description here