在继续之前等待响应
Wait response before proceeding
我正在开发一个 iOS Swift 应用程序,我正在尝试获取网站的内容。问题是,当我 运行 代码时,在执行其他行后返回响应对象。
我有这个:
public var response: Array<Any>?
public func myRequest(completion: @escaping (_ json: Any?, _ error: Error?)->()) {
var request = URLRequest(url: self.url)
request.httpBody = postString.data(using: .utf8)
let t = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data,
error == nil else {
completion(nil, error)
return
}
let json = try? JSONSerialization.jsonObject(with: data, options: [])
if let dictionary = json as? [String : Any] {
if (dictionary["ok"] as? String == "true") {
self.response = dictionary["users"] as? Array<Any>
}
}
completion(json, error)
}
t.resume()
}
然后:
func foo() {
myRequest() { json, error in
print(json)
}
print("I'm here!")
}
我得到这个:
I'm here
{...} //JSON
问题是:为什么我要在 JSON
之前检索 I'm here
?我该如何解决?
这是一个示例(基于您的代码)如何让 myRequest 接受完成块并在 JSON 被反序列化(或未反序列化)后调用它。
public func myRequest(completion: @escaping (_ json: Any?, _ error: Error?)->())
{
var request = URLRequest(url: self.url)
request.httpBody = postString.data(using: .utf8)
let t = URLSession.shared.dataTask(with: request)
{ data, response, error in
guard let data = data,
error == nil else
{
completion(nil, error)
return
}
let json = try? JSONSerialization.jsonObject(with: data, options: [])
//Do other things
completion(json, error)
}
t.resume()
}
您可以这样称呼它:
func foo()
{
myRequest()
{ json, error in
// will be called at either completion or at an error.
}
}
现在,如果您不在主线程上,并且真的想等待 myRequest() 完成,方法如下(顺便说一句,有很多方法可以做到这一点):
func foo()
{
let group = DispatchGroup()
group.enter()
myRequest()
{ json, error in
// will be called at either completion or at an error.
group.leave()
}
group.wait() // blocks current queue so beware!
}
我正在开发一个 iOS Swift 应用程序,我正在尝试获取网站的内容。问题是,当我 运行 代码时,在执行其他行后返回响应对象。
我有这个:
public var response: Array<Any>?
public func myRequest(completion: @escaping (_ json: Any?, _ error: Error?)->()) {
var request = URLRequest(url: self.url)
request.httpBody = postString.data(using: .utf8)
let t = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data,
error == nil else {
completion(nil, error)
return
}
let json = try? JSONSerialization.jsonObject(with: data, options: [])
if let dictionary = json as? [String : Any] {
if (dictionary["ok"] as? String == "true") {
self.response = dictionary["users"] as? Array<Any>
}
}
completion(json, error)
}
t.resume()
}
然后:
func foo() {
myRequest() { json, error in
print(json)
}
print("I'm here!")
}
我得到这个:
I'm here
{...} //JSON
问题是:为什么我要在 JSON
之前检索 I'm here
?我该如何解决?
这是一个示例(基于您的代码)如何让 myRequest 接受完成块并在 JSON 被反序列化(或未反序列化)后调用它。
public func myRequest(completion: @escaping (_ json: Any?, _ error: Error?)->())
{
var request = URLRequest(url: self.url)
request.httpBody = postString.data(using: .utf8)
let t = URLSession.shared.dataTask(with: request)
{ data, response, error in
guard let data = data,
error == nil else
{
completion(nil, error)
return
}
let json = try? JSONSerialization.jsonObject(with: data, options: [])
//Do other things
completion(json, error)
}
t.resume()
}
您可以这样称呼它:
func foo()
{
myRequest()
{ json, error in
// will be called at either completion or at an error.
}
}
现在,如果您不在主线程上,并且真的想等待 myRequest() 完成,方法如下(顺便说一句,有很多方法可以做到这一点):
func foo()
{
let group = DispatchGroup()
group.enter()
myRequest()
{ json, error in
// will be called at either completion or at an error.
group.leave()
}
group.wait() // blocks current queue so beware!
}