观察异步请求

编程入门 行业动态 更新时间:2024-10-28 03:29:29
本文介绍了观察异步请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对API进行了三个单独的调用.当所有三个调用完成时,我将汇总数据并使用它形成一个单独的模型对象.

I have three separate calls to APIs. When all three calls are complete, I am going to aggregate the data and use it to form a separate model object.

我认为我会使用属性观察器来完成此操作,但是我不知道如何实现.任何帮助或指导将不胜感激.

I figured I would use property observers to accomplish this but I have no idea how it would be implemented. Any help or guidance would be appreciated.

我创建了一个用于进行网络调用的模型对象,该模型将响应数据传递到转义的闭包中.这是解析数据的功能:

I have created a model object for making the network calls that passes the response data into an escaping closure. This is the function where the data is parsed:

func loadLibrary() { // League Data Containers var names = Dictionary<Int, String>() // API Call 1 var titles = Dictionary<Int, String>() // Call 1 var masteryLevels = Dictionary<Int, Int>() // 2 var masteryPoints = Dictionary<Int, Int>() // 2 // Champion Data Containers var championRolesLibrary = Array<Dictionary<String,Array<Role>>>() // 3 var championWithRoles = Dictionary<String,Array<Role>>() // 3 var championRoles = Array<Role>() // 3 // Making Calls to the APIs leagueAPI.downloadStaticData { data in // API Call is made and data is parsed into containers } leagueAPI.downloadChampionMasteryData { data in // API Call is made and data is parsed into containers } championAPI.downloadChampionRolesData { data in // API Call is made and data is parsed into containers } // Once all three calls have completed and the data has been parsed into different containers, the data is all brought together to create a library of objects. func aggregateData() { // Take data from all the containers and use them in here. // The issue is when to call this function. } }

推荐答案

一种简单的解决方法是嵌套所有三个请求:

A simple way to solve that is by nesting all three requests:

func loadLibrary() { // League Data Containers var names = Dictionary<Int, String>() // API Call 1 var titles = Dictionary<Int, String>() // Call 1 var masteryLevels = Dictionary<Int, Int>() // 2 var masteryPoints = Dictionary<Int, Int>() // 2 // Champion Data Containers var championRolesLibrary = Array<Dictionary<String,Array<Role>>>() // 3 var championWithRoles = Dictionary<String,Array<Role>>() // 3 var championRoles = Array<Role>() // 3 // Making Calls to the APIs leagueAPI.downloadStaticData { data in // API Call is made and data is parsed into containers leagueAPI.downloadChampionMasteryData { data2 in // API Call is made and data is parsed into containers championAPI.downloadChampionRolesData { data3 in // API Call is made and data is parsed into containers// Once all three calls have completed and the data has been parsed into different containers, the data is all brought together to create a library of objects. aggregateData() { // Take data from all the containers and use them in here. // The issue is when to call this function. } } } } }

您也可以使用@ rmaddy所说的DispatchGroup来完成所需的操作,在这种情况下,您可以这样做:

You can also accomplish what you want by using DispatchGroup as said by @ rmaddy, in this case you would do this:

func loadLibrary() { // League Data Containers var names = Dictionary<Int, String>() // API Call 1 var titles = Dictionary<Int, String>() // Call 1 var masteryLevels = Dictionary<Int, Int>() // 2 var masteryPoints = Dictionary<Int, Int>() // 2 // Champion Data Containers var championRolesLibrary = Array<Dictionary<String,Array<Role>>>() // 3 var championWithRoles = Dictionary<String,Array<Role>>() // 3 var championRoles = Array<Role>() // 3 // Create DispatchGroup let dispatchGroup = DispatchGroup() // Making Calls to the APIs dispatchGroup.enter() leagueAPI.downloadStaticData { data in // API Call is made and data is parsed into containers dispatchGroup.leave() } dispatchGroup.enter() leagueAPI.downloadChampionMasteryData { data in // API Call is made and data is parsed into containers dispatchGroup.leave() } dispatchGroup.enter() championAPI.downloadChampionRolesData { data in // API Call is made and data is parsed into containers dispatchGroup.leave() } // Once all three calls have completed and the data has been parsed into different containers, the data is all brought together to create a library of objects. dispatchGroup.notify(queue: DispatchQueue.global(qos: .background)){ aggregateData() { // Take data from all the containers and use them in here. // The issue is when to call this function. } } }

更多推荐

观察异步请求

本文发布于:2023-11-11 17:44:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579129.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!