DispatchGroup notify()不被调用

时间:2018-12-03 15:39:26

标签: ios swift asynchronous dispatch-async

我必须等待完成,直到用户触摸“重试”按钮以再次调用相同的功能。第一次都可以正常运行,但是当我再次按下它时,不会调用notfy()方法。这是模拟该场景的操场类:

import Foundation

class TokenManager{
    private var alertAlreadyShown = false
    static let shared = TokenManager()
    var alreadyEnterGroup = false
    let handlerTryAgainGroup = DispatchGroup()

    func showRefreshAlert(onTryAgainPressed : @escaping()->Void){
        //PREVENTS TO ENTER ON GROUP MUTIPLE TIMES (ASYNC CALLS)
        if(!alreadyEnterGroup){
            alreadyEnterGroup = true
            self.handlerTryAgainGroup.enter()
        }
        //PREVENTS TO SHOW ALERT MUTIPLE TIMES (ASYNC CALLS)
        if(!alertAlreadyShown){
            alertAlreadyShown = true
           //NOTIFY THE COMPLETION THAT USER TOUCH TRY AGAIN BUTTON
            handlerTryAgainGroup.notify(queue: DispatchQueue.main) {
                onTryAgainPressed()
            }
        }else{
            //THIS IS JUST A TEST TO SIMULATE THE USER TAPS MUTIPLE TIMES ON BUTTON
            TokenManager.shared.tryAgainPressed()
        }

    }

    func tryAgainPressed() {
        alreadyEnterGroup = false
        handlerTryAgainGroup.leave()
    }
}

func showAlert(){
    TokenManager.shared.showRefreshAlert {
        //CALLS THE SAME FUNCTION IF THE USER TAPS TRY AGAIN
         showAlert()
     }
}

//SHOW THE ALERT AND PRESS TRY AGAIN FOR THE FIRST TIME
showAlert()
TokenManager.shared.tryAgainPressed()

在这种情况下怎么了?

0 个答案:

没有答案