暂停主线程将异步函数快速运行

时间:2017-05-26 22:54:53

标签: swift multithreading firebase-realtime-database

我在while循环中调用以下函数,但即使使用dispatch_group,异步代码也不会运行。有谁知道我可能做错了什么? *我试图基本上获取while循环以等待异步代码在继续之前执行。

override func viewDidAppear(animated: Bool) {
    if(ended != true) {
        if(count2 < 2) {
            print("current count is < 2")

        } else {
            var playRef = ref.child("LiveGames").child("05-25-17").child("RedSox")

            asyncCall(playRef, ended: ended, count: count2)

            }
            print("successful delay")
            count2++

            //playIsntThere()

        }//this is where the else loop ends

        count2++
    }



 func asyncCall(ref:FIRDatabaseReference, ended:Bool, count:Int) {
        var count2 = count

        let myGroup = dispatch_group_create()
    dispatch_group_enter(myGroup)

        ref.child(String(count2-2)).observeSingleEventOfType(.Value, withBlock: { (snapshot2) in
            var hitterRef = snapshot2.childSnapshotForPath("/Hitter")
            var pitcherRef = snapshot2.childSnapshotForPath("/Pitcher")

            pitcherEra.text = String(pitcherRef.childSnapshotForPath("/ERA").value!)
        })
        dispatch_group_leave(myGroup)


        dispatch_group_notify(myGroup, dispatch_get_main_queue()) {
            print("code completed")
        }
        print("successful delay")
        count2++
    }

0 个答案:

没有答案