快速任务并行执行的问题 - 希望确保在警报窗口提供选择结果后执行我的代码

时间:2015-05-05 06:36:32

标签: swift parallel-processing execution

全部,我创建了以下功能:

func determineStatus() -> Bool {
    let status = ABAddressBookGetAuthorizationStatus()
    var ok = false
    switch status {
        case .NotDetermined:
            println("Asking for Grant")
            ABAddressBookRequestAccessWithCompletion(nil) {
                (granted:Bool, err:CFError!) in
                ok = granted
                println("Granted Status: \(granted)")
            }
        case .Authorized:
            println("Authorized")
            ok = true
        case .Restricted:
            println("Restricted")
            ok = false
        case .Denied:
            println("Denied")
            ok = false
    }
    if ok == true {
        println("Creating AB Instance")
        return self.createAddressBook()
    } else {
        self.adbk = nil
        println("Not Authorized")
        return false
    }

}

我从我的主人 - 详细信息视图中调用它。 我第一次调用它时,视图出现在要求授权的警报窗口之前,并且它是空的。 我进入我的控制台:

  

要求格兰特   未经授权   并且,在我授权之后:   授予状态:是的   因此,在ABAddressBookRequestAccessWithCompletion(nil)完成之前,确定yesStatsus的if ok == true部分。   我第二次运行它时会显示数据。   我如何更改determineStatus以便在ABAddressBookRequestAccessWithCompletion(nil)结束后执行if ok == true语句?

1 个答案:

答案 0 :(得分:0)

我找到了一个有效的解决方案here

积分转到@matt,我只是复制/粘贴他的代码并为此示例添加了对该类的调用:

class MyAddressBook {
    var adbk : ABAddressBook!

    func createAddressBook() -> Bool {
        if self.adbk != nil {
            return true
        }
        var err : Unmanaged<CFError>? = nil
        let adbk : ABAddressBook? = ABAddressBookCreateWithOptions(nil, &err).takeRetainedValue()
        if adbk == nil {
            println(err)
            self.adbk = nil
            return false
        }
        self.adbk = adbk
        return true
    }

    func determineStatus() -> Bool {
        let status = ABAddressBookGetAuthorizationStatus()
        switch status {
        case .Authorized:
            return self.createAddressBook()
        case .NotDetermined:
            var ok = false
            ABAddressBookRequestAccessWithCompletion(nil) {
                (granted:Bool, err:CFError!) in
                dispatch_async(dispatch_get_main_queue()) {
                    if granted {
                        ok = self.createAddressBook()
                    }
                }
            }
            if ok == true {
                return true
            }
            self.adbk = nil
            return false
        case .Restricted:
            self.adbk = nil
            return false
        case .Denied:
            self.adbk = nil
            return false
        }
    }

    func getContactNames() {
        if !self.determineStatus() {
            println("not authorized")
            return
        }
        let people = ABAddressBookCopyArrayOfAllPeople(adbk).takeRetainedValue() as NSArray as [ABRecord]
        for person in people {
            println(ABRecordCopyCompositeName(person).takeRetainedValue())
        }
    }
}

在其他地方,可能是在一个控制器中(我在一个简单的测试应用程序的viewDidLoad中进行了测试,它运行良好),执行此操作:

let myABInstance = MyAddressBook()
let result = myABInstance.determineStatus()
println("Succeeded: \(result)")
myABInstance.getContactNames()

我得到了什么:

true
Kate Bell
Daniel Higgins Jr.
John Appleseed
Anna Haro
Hank M. Zakroff
David Taylor