gpt4 book ai didi

ios - 在 URLSession.shared.dataTask 期间存储来自异步闭包的数据

转载 作者:行者123 更新时间:2023-11-30 12:26:03 26 4
gpt4 key购买 nike

我尝试了两种技术来获取数据并从完成处理程序填充数组。在这两种方法中,dataArray 计数都显示为 0。而我可以设置断点,并看到当执行在闭包内时数组正在填充:

尝试的第一种方法:

在下面的代码中,dataArray 显示计数为零,即使它在执行内部和外部完成处理程序期间填充 dataArray。

class ViewController: UIViewController { 
var dataArray = []
var urlOuter = URL(string: "outer.url.com/json")

override func viewDidLoad() {
super.viewDidLoad()
self.downloadTask()
print(dataArray.count)
}

func downloadTask() {

let outerTask = URLSession.shared.dataTask(with: urlOuter!, completionHandler: {
(data, response, error) in
let parsedData = try JSONSerialization.jsonObject(with: content, options: .mutableContainers) as! [[String: Any]]

for arr in parsedData! {
var urlInner = URL(string: "http://inner.url/" + arr["url"] + ".com/json")
let innerTask = URLSession.shared.dataTask(with: urlInner!, completionHandler: {
(data, response, error) in
let innerParsedData = try JSONSerialization.jsonObject(with: content, options: .mutableContainers) as! [[String: Any]]
self.dataArray.append(innerParsedData)

})
innerTask.resume()
}// end of for loop

})
outerTask.resume()
}

}

尝试的第二种方法:

protocol MyDelegate{ func didFetchData(data:String)}
class ViewController: UIViewController {
var dataArray = []
var urlOuter = URL(string: "outer.url.com/json")

override func viewDidLoad() {
super.viewDidLoad()
self.downloadTask()
print(dataArray.count)
}

func didFetchData(data:String) {
self.dataArray.append(data)
}

func downloadTask() {

let outerTask = URLSession.shared.dataTask(with: urlOuter!, completionHandler: {
(data, response, error) in
let parsedData = try JSONSerialization.jsonObject(with: content, options: .mutableContainers) as! [[String: Any]]

for arr in parsedData! {
var urlInner = URL(string: "http://inner.url/" + arr["url"] + ".com/json")
let innerTask = URLSession.shared.dataTask(with: urlInner!, completionHandler: {
(data, response, error) in
let innerParsedData = try JSONSerialization.jsonObject(with: content, options: .mutableContainers) as! String
self. didFetchData(data:innerParsedData)

})
innerTask.resume()
}// end of for loop

})
outerTask.resume()
}}}

请帮助我了解如何从闭包中获取数据并将其存储在数组中。建议的其他解决方案是使用委托(delegate),这就是我在方法 2 中尝试的方法。谢谢。

最佳答案

您在调用异步方法填充数组后,立即在 viewDidLoad 方法中查询该数组。

检查 didFetchData() 第二个方法中的结果。

override func viewDidLoad() {
super.viewDidLoad()
self.downloadTask()
}


func didFetchData(data:String) {
self.dataArray.append(data)
// Check the count here!!
print(dataArray.count)
}

您需要将协议(protocol)更改为:

protocol MyDelegate{ func didFetchData(dataArray: [])}

然后为委托(delegate)添加变量:

var mDelegate = MyDelegate?

然后分配你的结果:

func didFetchDataCompeleted(dataArray: []) {
// hand over the data to the delegate
mDelegate?.didFetchData(self.dataArray)
}

现在将闭包代码中完成 innerTask 时的调用更改为

self.didFetchDataCompeleted(dataArray:self.dataArray)

或直接调用:

self.mDelegate?.didFetchData(self.dataArray)

innerTask完成时

关于ios - 在 URLSession.shared.dataTask 期间存储来自异步闭包的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44206413/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com