【问题标题】:Semaphore to sync request Swift4 stopped forever信号量同步请求 Swift4 永远停止
【发布时间】:2018-03-21 12:41:56
【问题描述】:

我需要来自网络服务的同步请求,所以我使用信号量:

class func syncProducts() {
    print("syncProducts() 1")
    let idsLocal = getProductsIds()
    let semaphore = DispatchSemaphore(value: 0)
    var idsCloud : [Int] = []
    print("Getting cloud ids... 1")

    OdooService.getProductsIds { (params: [Int]) in
        print("SuccessBlock size ids: \(params.count)  1")
        idsCloud = params
        semaphore.signal()
    }

    semaphore.wait()
    print("Depois do GetproductsIds:  1")
}

但在此示例中,应用程序会永远保持锁定!请求永远不会结束。这是我从网络服务器请求数据并返回成功块的函数。

static func getProductsIds(successBlock: @escaping (_ params: [Int]) -> Void)  {
    // check odoo auth
    let dispatch = DispatchGroup()
    if( OdooAuth.uid == 0 ) {
        print("Odoo offline, tentando reconectar...")
        dispatch.enter()
        OdooAuth.reconnect(successBlock: { params in
            print("Reconectado com sucesso...")
            dispatch.leave()
        }, failureBlock: { params in
            print("Falha no ReAuth")
            return
        })
    }

    print("Start request from Odoo...")
    let fieldsProducts = ["id"]
    let options = [ "fields": fieldsProducts] as [String : Any]
    var idsList : [Int] = []
    let params = [OdooAuth.db, OdooAuth.uid, OdooAuth.password,"product.template","search_read",[],options] as [Any]

    AlamofireXMLRPC.request(OdooAuth.host2, methodName: "execute_kw", parameters: params).responseXMLRPC {
        (response: DataResponse<XMLRPCNode>) -> Void in
        switch response.result {
        case .success( _):
            print("Success to get Ids")
            let str = String(data: response.data!, encoding: String.Encoding.utf8) as String!
            let options = AEXMLOptions()
            let xmlDoc = try? AEXMLDocument(xml: (str?.data(using: .utf8))!,options: options)
            //print(xmlDoc!.xml)

            for child in (xmlDoc?.root["params"]["param"]["value"]["array"]["data"].children)! {
                for childValue in child["struct"].children {
                    let id = childValue["value"]["int"].value!
                    idsList.append(Int(id)!)
                    //print("Id: \(id)")
                }
            }

            successBlock(idsList)

            break

        case .failure(let error):
                print("Error to get Ids: \(error.localizedDescription)")
            break
        } // fim switch
    } // fim request
} // fim getProductsIds

我不知道信号量是否是最好的方法,但我需要同步请求!我尝试在 reauth 中使用 DispatchGroup(),但也不起作用。

【问题讨论】:

    标签: ios swift semaphore


    【解决方案1】:

    我希望死锁是在主线程上调用getProductsIds 回调的结果,该回调被信号量阻塞。据我所知,默认情况下,Alamofire 在主线程上调度回调,我认为 AlamofireXMLRPC 就是这种情况,因为它是 Alamofire 的包装器。

    我强烈建议不要在异步操作期间阻塞主线程。

    但是,如果出于任何非常好的理由您不能这样做,则需要确保回调不会在主调度队列上被调度(因为该队列被阻塞等待信号)。 Alamofire 本身有一个response 重载,允许指定在其上运行回调的DispatchQueue 对象。似乎 AlamofireXMLRPC 也有一个,所以我会尝试利用它并进行更改

    AlamofireXMLRPC.request(OdooAuth.host2, methodName: "execute_kw", parameters: params)
                   .responseXMLRPC {
         // process result
    }
    

    到:

    AlamofireXMLRPC.request(OdooAuth.host2, methodName: "execute_kw", parameters: params)
                   .responseXMLRPC(queue: DispatchQueue.global(qos: .background)) {
         // process result
    }
    

    我是基于AlamofireXMLRPC的githubsource code,但是之前没用过,所以可能会有一些语法错误。但它应该为您指明正确的方向。尽管如此,我还是建议您不要阻塞线程(我在重复自己,但这确实非常重要)。

    【讨论】:

    • 真的!这看起来很神奇!工作正常!我只是将参数添加到请求中:AlamofireXMLRPC.request(OdooAuth.host2, methodName: "execute_kw", parameters: params).responseXMLRPC(queue: DispatchQueue.global(qos: .background)) { (response: DataResponse&lt;XMLRPCNode&gt;) -&gt; Void in ...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 2019-07-14
    • 2013-05-30
    • 1970-01-01
    相关资源
    最近更新 更多