Swift URL请求消耗CPU

 收藏

当我在快速应用程序中发出url请求时,一切正常,我得到了结果,但是当我断开互联网连接(ps请求已完成并得到结果)时,cpu升高到了99%,并在一段时间后降低了 怎么了 ??

let session = URLSession.shared
let url = URL(string: "https://somehost.com/users.json")!

let task = session.dataTask(with: url) { data, response, error in

    if error != nil || data == nil {
        print("Client error!")
        return
    }

    guard let response = response as? HTTPURLResponse, (200...299).contains(response.statusCode) else {
        print("Server error!")
        return
    }

    guard let mime = response.mimeType, mime == "application/json" else {
        print("Wrong MIME type!")
        return
    }

    do {
        let json = try JSONSerialization.jsonObject(with: data!, options: [])
        print(json)
    } catch {
        print("JSON error: \(error.localizedDescription)")
    }        
}

task.resume()
回复