首页 > 解决方案 > 执行多个 Alamofire 请求时出现错误 (500)

问题描述

当我运行这段代码时,我得到了 500 分。但是,如果我只运行一个请求,它工作得很好

我在操场上的代码:

import UIKit
import Alamofire


var url : String = "http://localhost:5000/"

var fieldid: String = "jQNguWfyGowlS3QoOI2vTWcX71RUjsZg"

var resreturn: [String: [Double]] = ["temperature":[], "humidity":[], "ph":[], "light":[]]
var resresreturn: [[String: [Double]]] = []
let dispatch2 = DispatchGroup()
dispatch2.enter()
resresreturn = []
let dispatch = DispatchGroup()
dispatch.enter()
dispatch.enter()
let myurltemp = url + "aktionsdaten/temperatur/" + fieldid
Alamofire.request(myurltemp, method: .get).responseJSON{ response1 in

    if let status = response1.response?.statusCode {
        switch(status){
        case 201, 200:
            print("example success")
        default:
            print("error 1 with response status: \(status) at endpont \(myurltemp)")
            return
        }
    }

    if let result = response1.result.value {
        resreturn["ph"] = result as! NSArray as? [Double] ?? [0.0,0.0]
        dispatch.leave()
    }
}
dispatch.enter()
let myurllight = url + "aktionsdaten/light/" + fieldid
Alamofire.request(myurllight, method: .get).responseJSON{ response2 in

    if let status = response2.response?.statusCode {
        switch(status){
        case 201, 200:
            print("example success")
        default:
            print("error 1 with response status: \(status) at endpont \(myurllight)")
            return
        }
    }

    if let result = response2.result.value {
        resreturn["ph"] = result as! NSArray as? [Double] ?? [0.0,0.0]
        dispatch.leave()
    }
}
dispatch.enter()
let myurlakt = url + "aktionsdaten/ph/" + fieldid
Alamofire.request(myurlakt, method: .get).responseJSON{ response3 in

    if let status = response3.response?.statusCode {
        switch(status){
        case 201, 200:
            print("example success")
        default:
            print("error 1 with response status: \(status) at endpont \(myurlakt)")
            return
        }
    }

    if let result = response3.result.value {
        resreturn["ph"] = result as! NSArray as? [Double] ?? [0.0,0.0]
        dispatch.leave()
    }
}
let myurlhum = url + "aktionsdaten/feuchtigkeit/" + fieldid
Alamofire.request(myurlhum, method: .get).responseJSON{ response4 in

    if let status = response4.response?.statusCode {
        switch(status){
        case 201, 200:
            print("example success")
        default:
            print("error 1 with response status: \(status) at endpont \(myurlhum)")
            return
        }
    }

    if let result = response4.result.value {
        resreturn["ph"] = result as! NSArray as? [Double] ?? [0.0,0.0]
        dispatch.leave()
    }
}
dispatch.notify(queue: .main, execute: {
    resresreturn.append(resreturn)
    dispatch2.leave()
})

错误日志:

错误 1 ​​响应状态:500 在 endpiont http://localhost:5000/aktionsdaten/temperatur/jQNguWfyGowlS3QoOI2vTWcX71RUjsZg 错误1 响应状态:500 在 endpiont http://localhost:5000/aktionsdaten/light/jQNguWfyGowlS3QoOI2vTWcX71RUjsZg 错误1 响应状态: 500 在 endipont http://localhost:5000/aktionsdaten/feuchtigkeit/jQNguWfyGowlS3QoOI2vTWcX71RUjsZg 错误 1 ​​响应状态:500 在 endipont http://localhost:5000/aktionsdaten/ph/jQNguWfyGowlS3QoOI2vTWcX71RUjsZg

当我只运行一个请求时,我得到: example success

标签: iosswiftalamofire

解决方案


推荐阅读