dispatch_after函数永远不会运行

时间:2017-05-24 23:26:30

标签: ios swift firebase firebase-realtime-database

我正在尝试执行以下代码,但是dispatch_after方法中的代码没有运行。在我运行的实例中,else条件将始终为true,如果我在dispatch_after之前放置一个print语句,则按预期打印。是否有人可以看到会阻止它运行?

func afterAsyncRun(ref:FIRDatabaseReference, ended:Bool, count:Int, batterName:UILabel, pitcherName:UILabel) {
    var count2 = count
    while(ended != true) {
        if(count2 < 2) {
            print("current count is < 2")

        } else {
            let seconds = 2.0
            let delay = seconds * Double(NSEC_PER_SEC)  // nanoseconds per seconds
            let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

            dispatch_after(dispatchTime, dispatch_get_main_queue(), {

                //below is the delayed code
                ref.child(String(count-2)).observeSingleEventOfType(.Value, withBlock: { (snapshot2) in
                    var hitterRef = snapshot2.childSnapshotForPath("/Hitter")
                    var pitcherRef = snapshot2.childSnapshotForPath("/Pitcher")
                    batterName.text = (String(hitterRef.childSnapshotForPath("/fName")) + " " + String(hitterRef.childSnapshotForPath("/lname")))
                    pitcherName.text = (String(pitcherRef.childSnapshotForPath("/fName")) + " " + String(pitcherRef.childSnapshotForPath("/lname")))

                print("successful delay")

                })
                count2++

            })//this is where the delay code ends

        }//this is where the else loop ends
    }//while loops ends
    print("labels updated")
}

当我没有纳秒次数乘数运行时,仍然没有任何反应。但是,等待几秒钟并终止运行后,我收到以下错误:

2017-05-25 10:10:55.191 Test[31038:2390189] XPC connection interrupted
2017-05-25 10:10:55.167 Test[31038:2390038] Communications error: <OS_xpc_error: <error: 0x106fb1b40> { count = 1, contents =
    "XPCErrorDescription" => <string: 0x106fb1ef0> { length = 22, contents = "Connection interrupted" }
}>

0 个答案:

没有答案