首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >观察异步请求

观察异步请求
EN

Stack Overflow用户
提问于 2017-04-28 22:25:52
回答 1查看 69关注 0票数 0

我对API有三个单独的调用。当三个调用都完成后,我将聚合数据并使用它形成一个单独的模型对象。

我想我会用财产观察者来完成这个任务,但我不知道它将如何实现。如有任何帮助或指导,将不胜感激。

我创建了一个模型对象,用于进行网络调用,将响应数据传递给转义闭包。这是解析数据的函数:

代码语言:javascript
复制
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.

    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-28 22:49:48

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

代码语言:javascript
复制
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.

                }
            }
        }
    }

}

编辑:您也可以通过使用DispatchGroup来完成您想要的事情,就像@ rmaddy所说的那样,在本例中您可以这样做:

代码语言:javascript
复制
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.

        }
    }

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43689685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档