同步调度不起作用?

Synchronous Dispatch Doesn't Work?

也许我不理解这个概念,但是第二段代码不应该在第一段完成之前等待吗?

dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> () in
    // do some task
    var myRef = self.userRef.childByAppendingPath(self.userRef.authData.uid)
    myRef.observeSingleEventOfType(.Value, withBlock: { snapshot in
        println("WE ARE IN HERE")
        firstName = snapshot.value["firstName"] as String!
        lastName = snapshot.value["lastName"] as String!
    })
});

dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> () in
    // do some task
    var standbyData = [
        "firstName":firstName,
        "lastName":lastName,
        "duration":self.helpDurationLabel.text!,
        "radius":self.helpRadiusLabel.text!,
        "categories": categories,
        "time_stamp":self.date.timeIntervalSince1970
    ]
    println("WE ARE OUT HERE")
    var standbyUserRef = self.standbyRef.childByAppendingPath(self.userRef.authData.uid)
    standbyUserRef.updateChildValues(standbyData)
});

有什么我遗漏的吗?

输出为:

"WE ARE OUT HERE"
"WE ARE IN HERE"

它会等到第一个完成执行。系统让您感到困惑的地方在于您对 myRef.observeSingleEventOfType(, withBlock:

的使用

99% 请求执行块的 API 会将其操作推送到单独的线程,然后在完成时执行该块。 observeSingleEventOfType 可能在将其执行安排到操作队列后不久返回,导致您的 dispatch_sync 在块执行之前退出。

相反,您可能正在寻找更像这样的东西:

dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> () in
    // do some task
    var myRef = self.userRef.childByAppendingPath(self.userRef.authData.uid)
    myRef.observeSingleEventOfType(.Value, withBlock: { snapshot in
        println("WE ARE IN HERE")
        firstName = snapshot.value["firstName"] as String!
        lastName = snapshot.value["lastName"] as String!

        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> () in
            // do some task
            var standbyData = [
                "firstName":firstName,
                "lastName":lastName,
                "duration":self.helpDurationLabel.text!,
                "radius":self.helpRadiusLabel.text!,
                "categories": categories,
                "time_stamp":self.date.timeIntervalSince1970
            ]
            println("WE ARE OUT HERE")
            var standbyUserRef = self.standbyRef.childByAppendingPath(self.userRef.authData.uid)
            standbyUserRef.updateChildValues(standbyData)
        });
    })
});

dispatch_get_global_queue 用于 运行 任务在后台并发意味着任何任务都可以 运行 先行。当您打印 "we are in" 和 "we are out" 时,意味着任何人都可以先 运行。 相反,我建议 运行 连续而不是同时执行任务 dispatch_queue_create 用于连续 运行 任务。 我希望下面的代码能帮助解决这个问题。

var backgroundQueue:dispatch_queue_t =  dispatch_queue_create("SerialQueue", DISPATCH_QUEUE_SERIAL)

        dispatch_sync(backgroundQueue, { () -> () in
            // do some task
            var myRef = self.userRef.childByAppendingPath(self.userRef.authData.uid)
            myRef.observeSingleEventOfType(.Value, withBlock: { snapshot in
                println("WE ARE IN HERE")
                firstName = snapshot.value["firstName"] as String!
                lastName = snapshot.value["lastName"] as String!
            })
        });

        dispatch_sync(backgroundQueue, { () -> () in
            // do some task
            var standbyData = [
                "firstName":firstName,
                "lastName":lastName,
                "duration":self.helpDurationLabel.text!,
                "radius":self.helpRadiusLabel.text!,
                "categories": categories,
                "time_stamp":self.date.timeIntervalSince1970
            ]
            println("WE ARE OUT HERE")
            var standbyUserRef = self.standbyRef.childByAppendingPath(self.userRef.authData.uid)
            standbyUserRef.updateChildValues(standbyData)
        });