【问题标题】:DispatchGroup with async task具有异步任务的 DispatchGroup
【发布时间】:2018-10-22 08:57:30
【问题描述】:

enter code here我在调度组尝试了很多东西,但我无法获得稳定的结果。 从我的服务器开始,我使用 Alamofire 获取数据。我在 Helper Class 中编写了一个函数,并在 AppDelegate.swift 中使用了这个函数。

不知道是在AppDelegate调用函数的时候把dispatch group放在了,还是把dispatch group只放在了Helper Class的函数里。

    func alomofireGet(URL: String, onCompletion:@escaping ((JSON) -> Void)) {
   // let group = DispatchGroup()
    var contentJSON = JSON()
   // group.enter()
    Alamofire.request(URL, method: .get).responseJSON() { (reponse) in
        if reponse.result.isSuccess {
            contentJSON = JSON(reponse.result.value!)
        } else {
            contentJSON = JSON(reponse.result.error!)
        }
     //   group.leave()
    }
  //  group.notify(queue: .main) {
        onCompletion(contentJSON)
}

在 App 委托中,我编写了一个函数,该函数在我的类中调用该函数。

    func connect() {
    let group = DispatchGroup()
    let _: Bool = KeychainWrapper.standard.removeObject(forKey: "token")
    var token = String()
    group.enter()
    Helper().alomofireGet(URL: "http://192.168.1.19/app_dev.php/login/app") { contenuJSON in
        token = contenuJSON["csrfToken"].stringValue
        group.leave()
    }
    group.notify(queue: .main) {
        let _: Bool = KeychainWrapper.standard.set(token, forKey: "token")
        let t: String? = KeychainWrapper.standard.string(forKey: "token")
        print(t!)
   }
}

问题是变量“t”为空。 当我在应用程序委托中调用 keychainWrapper 时,钥匙串也是空的。

PS:我还有其他任务,我只是减少了我的代码

【问题讨论】:

  • 那么,你的问题到底是什么?
  • DispatchGroup 对于 single 异步任务毫无意义。将 notify 正文中的代码放入 Helper 完成块中。
  • 我已经更新了我的回复谢谢
  • 我还有其他任务,我只是减少了我的代码
  • 还原证伪问题。

标签: swift concurrency dispatch


【解决方案1】:
func alomofireGet(URL: String, onCompletion:@escaping ((JSON) -> Void)) {
   // let group = DispatchGroup()
    var contentJSON = JSON()
   // group.enter()
    Alamofire.request(URL, method: .get).responseJSON() { (reponse) in
        if reponse.result.isSuccess {
            contentJSON = JSON(reponse.result.value!)
        } else {
            contentJSON = JSON(reponse.result.error!)
        }
     //   group.leave()
    }
  //  group.notify(queue: .main) {// where you call wait()function. This blocks the current thread until the group’s tasks have completed.
        onCompletion(contentJSON)
}

【讨论】:

  • 我不调用等待函数。也许是问题?
  • 我使用 group.notify
【解决方案2】:

我试试这个,但不是解决方案。我已经删除了 Helper 中的功能。我在应用程序委托中有这个功能。

func connect(onCompletion : @escaping (String) -> ()) {
    let group = DispatchGroup()
    var token = String()
    let _: Bool = KeychainWrapper.standard.removeObject(forKey: "token")
    group.enter()
    Alamofire.request("http://192.168.1.19/app_dev.php/login/app", method: .get).responseJSON() { (reponse) in
        if reponse.result.isSuccess {
            let contentJSON = JSON(reponse.result.value!)
            token = contentJSON["csrfToken"].stringValue
        } else {
            token = "Invalid Token"
        }
       group.leave()
    }
    group.notify(queue : DispatchQueue.global()) {
        onCompletion(token)
    }
}

当我打印令牌时,我有一条空消息。

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    Thread.sleep(forTimeInterval: 1.5)
    connect() { token in
        print(token)
    }
    return true
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多