{"id":1185,"date":"2020-11-05T13:20:20","date_gmt":"2020-11-05T05:20:20","guid":{"rendered":"http:\/\/123.57.164.21\/?p=1185"},"modified":"2020-11-05T13:20:20","modified_gmt":"2020-11-05T05:20:20","slug":"session-datatask-%e5%90%8c%e6%ad%a5","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=1185","title":{"rendered":"session dataTask \u540c\u6b65"},"content":{"rendered":"\n<p>session dataTask\u901a\u5e38\u662f\u5f02\u6b65\u53d6\u56de\u7ed3\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7semaphore\u628a\u5f02\u6b65\u53d8\u6210\u540c\u6b65<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/\u521b\u5efaURL\u5bf9\u8c61\nlet urlString:String=\"http:\/\/www.hangge.com\"\nlet url = URL(string:urlString)\n\/\/\u521b\u5efa\u8bf7\u6c42\u5bf9\u8c61\nlet request = URLRequest(url: url!)\n \n\/\/ URLSession.shared \u91cd\u8981\nlet session = URLSession.shared\n \nlet semaphore = DispatchSemaphore(value: 0)\n \nlet dataTask = session.dataTask(with: request,\n                   completionHandler: {(data, response, error) -> Void in\n                    if error != nil{\n                        print(error!)\n                    }else{\n                        let str = String(data: data!, encoding: String.Encoding.utf8)\n                        print(str!)\n                    }\n                     \n                    semaphore.signal()\n}) as URLSessionTask\n \n\/\/\u4f7f\u7528resume\u65b9\u6cd5\u542f\u52a8\u4efb\u52a1\ndataTask.resume()\n \n_ = semaphore.wait(timeout: DispatchTime.distantFuture)\nprint(\"\u6570\u636e\u52a0\u8f7d\u5b8c\u6bd5\uff01\")\n\/\/\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u4ee3\u7801.......<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>session dataTask\u901a\u5e38\u662f\u5f02\u6b65\u53d6\u56de\u7ed3\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7semaphore\u628a\u5f02\u6b65\u53d8\u6210\u540c\u6b65<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1185"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1185"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1185\/revisions"}],"predecessor-version":[{"id":1186,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/1185\/revisions\/1186"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}