Ios 如何按节异步更新获取的json数据到tableView

Ios 如何按节异步更新获取的json数据到tableView,ios,json,swift,asynchronous,Ios,Json,Swift,Asynchronous,我从3个URL异步获取json数据。我将jsonDecoder的结果附加到数组中,每次启动我的应用程序时,它们的顺序都会改变,因为它是异步的。 因此,我将url保存到数组url=[“url1”、“url2”、“url3”],我真的很想将每个url的数组url索引更新到tableView的部分,该部分的索引与url的索引相同 网络模块类 jsonFilenames.forEach{(名称)在 让baseUrl=“https://~~//” guard let url=url(字符串:baseUrl

我从3个URL异步获取json数据。我将jsonDecoder的结果附加到数组中,每次启动我的应用程序时,它们的顺序都会改变,因为它是异步的。 因此,我将url保存到数组
url=[“url1”、“url2”、“url3”]
,我真的很想将每个
url
的数组
url
索引更新到tableView的部分,该部分的索引与
url
的索引相同

网络模块类

jsonFilenames.forEach{(名称)在
让baseUrl=“https://~~//”
guard let url=url(字符串:baseUrl+name)else{return}
中的session.dataTask(带有:url){(数据、响应、错误)
guard let data=data else{return}
让jsonDecoder=jsonDecoder()
jsonDecoder.keyDecodingStrategy=.convertFromSnakeCase
做{
让baseAPIResponse=尝试jsonDecoder.decode(baseAPIResponse.self,from:data)
NotificationCenter.default.post(名称:NSNotification.name(rawValue:“getData”),对象:nil,用户信息:[“json”:baseAPIResponse.body,“section”:name])
}接球手{
打印(“解码json时出错”,JSONER)
}
}1.简历()
}
通知

override func viewDidLoad(){
super.viewDidLoad()
NotificationCenter.default.addObserver(self,选择器:#选择器(onDidReceiveData),名称:NSNotification.name(rawValue:“getData”),对象:nil)
tableview.dataSource=tableViewModel
tableview.delegate=self
tableview.separatorStyle=.none
}
私有变量headerSections=[String]()
@objc fileprivate func onDidReceiveData(uu通知:通知){
guard let userInfoDict=notification.userInfo else{return}
将storeItems=userInfoDict[“json”]设为![StoreItem]
tableViewModel.storeItemsArray.append(storeItems)
DispatchQueue.main.async{
self.tableview.reloadData()
}
}
提示:如果此获取在同一个vc中进行,则不应使用notificationCenter,而应直接在回调中重新加载表

为此,您需要获取
name
的索引,该索引将返回需要在节的嵌套数组中插入返回数组的正确位置,并将其发送到notificationcenter中的
object
参数中

let baseAPIResponse = try jsonDecoder.decode(BaseAPIResponse<StoreItem>.self, from: data)
let index = jsonFilenames.index(of:name)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "getData"), object: "\(index)", userInfo: ["json": baseAPIResponse.body, "section": name])
let baseAPIResponse=尝试jsonDecoder.decode(baseAPIResponse.self,from:data)
让index=jsonFilenames.index(of:name)
NotificationCenter.default.post(名称:NSNotification.name(rawValue:“getData”),对象:“\(index)”,用户信息:[“json”:baseAPIResponse.body,“section”:name])
您可以使用来管理异步任务。并等待所有任务完成,然后合成任务收集的所有结果。然后,发送通知

您可以尝试以下方法。注:未经测试。我只想演示一下如何使用dispatch group来管理异步任务,并按照所需的正确顺序聚合结果

DispatchQueue.global(qos:.userInitiated).async{
let downloadGroup=DispatchGroup()
let results:[String]=数组(重复:“”,计数:jsonFilenames.count)
用于jsonFilenames.enumerated()中的(索引、名称){
让baseUrl=“https://~~//”
guard let url=url(字符串:baseUrl+name)else{return}
downloadGroup.enter()
中的session.dataTask(带有:url){(数据、响应、错误)
guard let data=data else{return}
让jsonDecoder=jsonDecoder()
jsonDecoder.keyDecodingStrategy=.convertFromSnakeCase
做{
让baseAPIResponse=尝试jsonDecoder.decode(baseAPIResponse.self,from:data)
结果[索引](baseAPIResponse.body)
downloadGroup.leave()
}接球手{
打印(“解码json时出错”,JSONER)
}
}1.简历()
}
//它将等待所有任务完成后再继续,并阻止当前队列。
//所以最好在main以外的另一个队列中执行代码,这就是原因
//为什么代码在DispatchQueue.global内执行
downloadGroup.wait()
//如果您的所有任务都成功完成,结果将按正确顺序排列。
//结果[0]是您第一个任务的结果。结果[1]将是第二个。依此类推。。。
NotificationCenter.default.post(名称:NSNotification.name(rawValue:“getData”),对象:nil,用户信息:[“json”:结果,“section”:名称])
}

谢谢,现在我知道了索引,但是为什么要像这样附加节数组
tableViewModel.storeItemsArray.append(storeItems)
,我不知道还有什么其他方法可以重新加载tableView。tableViewModel是UITableViewDataSource.Do
tableViewModel.storeItemsArray.inser(storeItems,at:index)
但请注意
storeItemsArray
创建的嵌套数组大小必须与urlsfirst相同,无需
DispatchQueue.global(qos:.userInitiated).async
second
DispatchGroup
不需要,因为它对部分用户没有帮助update@Sh_Khan根据我自己的理解,他希望显示结果与jsonFilenames异步结果的顺序一致。所以,我想DispatchGroup将是一个不错的选择。如果使用DispatchGroup,为什么您认为不需要
DispatchQueue.global(qos:.userInitiated).async
?我很想知道。谢谢。
session.dataTask(带:url){(数据、响应、错误)in
在后台线程中运行,因此不需要全局queue@Sh_Khan是的,正确,这就是每个异步任务。我们讨论的是当您使用
DispatchGroup
时,当您调用
wait
时,它会同步等待以前提交的工作完成。因此,它会阻止当前队列。如果您使用
DispatchGroup在主区中