Управление данными, загруженными с url - swift - PullRequest
0 голосов
/ 11 апреля 2019

У меня есть два URL-адреса для загрузки данных, и они дают мне JSON такого типа:

1) http://192.168.178.77/MyWebService/api/getteams.php

R ->

{"teams":
[{"id":1,"name":"Avengers","member":7},{"id":2,"name":"Fantastics4","member":4},{"id":3,"name":"test","member":6},{"id":4,"name":"teamtest","member":33},{"id":5,"name":"inserisco","member":33},{"id":6,"name":"sarto","member":33},
{"id":7,"name":"name","member":22},
{"id":8,"name":"test","member":999}]
}

2) http://127.0.0.1/MyWebService/api/fetch_image.php?id_team=1 // Где id_team param - это идентификатор, загруженный с первого URL

R -> {"image_team":[{"id":4,"img_path":"http:\/\/localhost\/MyWebService\/images\/imgTest.png","id_team":1}]}

Теперь, чтобы загрузить эти данные, я быстро создал классы и функции для хранения и загрузки данных.

2 класса, расположенные в соответствующем файле .swift:

1) в TeamJson.swift:

import Foundation

class ClassJsonTeam: Codable {

    private var teams: [JsonTeam]

    init(teams: [JsonTeam]) {
        self.teams = teams
    }

    func getTeams()-> [JsonTeam]{
        return(self.teams);
    }

    func setTeams(teams:[JsonTeam]){
        self.teams = teams;
    }

}

class JsonTeam: Codable {
    private let id: Int
    private var name: String
    private var member: Int

    init(id: Int, name: String, member: Int) {
        self.id = id
        self.name = name
        self.member = member
    }

    func getId()->Int{
        return(self.id);
    }

    func setId(id:Int){
        self.member = id;
    }

    func getName()->String{
        return(self.name);
    }

    func setName(name:String){
        self.name = name;
    }

    func getMembers()->Int{
        return(self.member);
    }

    func setMembers(members:Int){
        self.member = members;
    }
}

2) в ImageJsonTeam.swift

import Foundation

class ClassJsonTeamImage : Codable {
    private var teams : [JsonTeamImg]

    init(teams: [JsonTeamImg]) {
        self.teams = teams
    }

    func getTeamsImg()-> [JsonTeamImg]{
        return(self.teams);
    }

    func setTeamsImg(teams:[JsonTeamImg]){
        self.teams = teams;
    }
}

class JsonTeamImg : Codable{
    private var id : Int
    private var imagePath: URL
    private var teamId : Int

    init(id : Int, imagePath : URL , teamId : Int) {
        self.id = id
        self.imagePath = imagePath
        self.teamId = teamId
    }

    func getId() -> Int{
        return(self.id)
    }

    func setId(id : Int){
        self.id = id
    }

    func  getImagePath() -> URL {
        return(self.imagePath)
    }

    func setImagePath(imagePath : URL){
        self.imagePath = imagePath
    }

    func getTeamId()-> Int{
        return(self.teamId)
    }

    func setTeamId(teamId : Int){
        self.teamId = teamId
    }
}

Более того, поскольку мне нужно объединить информацию из двух jsons, я создал третий класс этого типа:

3) CompleteTeamJson.swift

import Foundation

public class ClassJsonCompleteTeam{
    private var team : [JsonCompleteTeam]

    init(team : [JsonCompleteTeam]){
        self.team = team
    }
}

public class JsonCompleteTeam{
    private var id : Int
    private var name : String
    private var member : Int
    private var imgUrl : URL

    init(id: Int, name: String, member: Int, imgUrl: URL) {
        self.id = id
        self.name = name
        self.member = member
        self.imgUrl = imgUrl
    }

    func getId()->Int{
        return(self.id);
    }

    func setId(id:Int){
        self.member = id;
    }

    func getName()->String{
        return(self.name);
    }

    func setName(name:String){
        self.name = name;
    }

    func getMembers()->Int{
        return(self.member);
    }

    func setMembers(members:Int){
        self.member = members;
    }

    func  getImageUrl() -> URL {
        return(self.imgUrl)
    }

    func setImagePath(imgUrl : URL){
        self.imgUrl = imgUrl
    }

}

Теперь ситуация в моем viewController такова:

import UIKit

//var teamCollection  : ClassJsonTeam!
//
class ViewController: UIViewController {

    @IBAction func InsertNewTeamButton(_ sender: Any) {
        performSegue(withIdentifier: "NewTeamSegue", sender: self)
    }

    var teams: [JsonTeam]?
    var teamsImg : [JsonTeamImg]?
    var teamsComplete : [JsonCompleteTeam]?

    override func viewDidLoad() {
        super.viewDidLoad()

        downloadTeams(completion: { (teams) in

            self.teams = teams

            for index in (0...(self.teams!.count - 1)){

                self.downloadTeamsImage(idTeam: teams[index].getId(), completion:
                    {(teamsImg) in self.teamsImg = teamsImg
                })
                let finalId : Int = teams[index].getId()
                let finalName : String = teams[index].getName()
                let finalMember : Int = teams[index].getMembers()
                var finalURL : URL = URL(string : "http://localhost/MyWebService/images/EmptyImg.png")!

                for index2 in (0...(self.teamsImg!.count - 1)){
                    if (finalId == self.teamsImg![index2].getId()){
                        finalURL = self.teamsImg![index2].getImagePath()
                    }
                }

                let tempCompleteTeam = JsonCompleteTeam(id: finalId, name: finalName, member: finalMember, imgUrl: finalURL)
                self.teamsComplete?.append(tempCompleteTeam)


            }

            //self.InsertNewTeamButton((Any).self)
            self.showTeamButton((Any).self)

        })
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // somethings in new wc //
    }


    @IBAction func showTeamButton(_ sender: Any) {
        performSegue(withIdentifier: "TeamListSegue", sender: self)
    }

    func downloadTeams(completion: @escaping (([JsonTeam]) -> Void)) {
        //get teams with classes
        let getTeamUrl = "http://192.168.178.77/MyWebService/api/getteams.php"

        guard let urlTeam = URL(string: getTeamUrl) else { return }
        URLSession.shared.dataTask(with: urlTeam) { (data,response,err) in
            guard let data = data else {return}
            do {
                let team = try JSONDecoder().decode(ClassJsonTeam.self, from: data)
                var tempArrayTeam = [JsonTeam]()
                for index in 0...(team.getTeams().count - 1) {

                    let tempTeam = JsonTeam(id: team.getTeams()[index].getId(),
                                            name: team.getTeams()[index].getName(),
                                            member: team.getTeams()[index].getMembers())

                    print(team.getTeams()[index].getId())
                    print(team.getTeams()[index].getName())
                    print(team.getTeams()[index].getMembers())

                    tempArrayTeam.append(tempTeam)
                }
                completion(tempArrayTeam)
            } catch let jsonErr{
                print("Error serializing json: \(jsonErr.localizedDescription)")
            }
            }.resume()
    }

    func downloadTeamsImage(idTeam: Int,completion: @escaping (([JsonTeamImg]) -> Void)) {
        //get teams with classes
        let getTeamUrl = "http://127.0.0.1/MyWebService/api/fetch_image.php?id_team=\(idTeam)"

        guard let urlTeam = URL(string: getTeamUrl) else { return }
        URLSession.shared.dataTask(with: urlTeam) { (data,response,err) in
            guard let data = data else {return}
            do {
                let team = try JSONDecoder().decode(ClassJsonTeamImage.self, from: data)
                var tempArrayTeamImg = [JsonTeamImg]()
                for index in 0...(team.getTeamsImg().count - 1) {

                    let tempTeamImg = JsonTeamImg(id: team.getTeamsImg()[index].getId(),
                                               imagePath: team.getTeamsImg()[index].getImagePath(),
                                               teamId: team.getTeamsImg()[index].getTeamId())

                    print(tempTeamImg.getId())
                    print(tempTeamImg.getImagePath())
                    print(tempTeamImg.getTeamId())

                    tempArrayTeamImg.append(tempTeamImg)
                }
                completion(tempArrayTeamImg)
            } catch let jsonErr{
                print("Error serializing json: \(jsonErr.localizedDescription)")
            }
            }.resume()
    }

}

Давайте подойдем к делу: если я вызываю две функции по отдельности, конечно, они работают, но я не знаю, как включить данные, загруженные в do-catch вне этого (например, в две команды переменных и в teamImg), чтобы мы могли добавить переменную teamComplete и перенести данные в другой контроллер представления.

Заранее спасибо: D

1 Ответ

0 голосов
/ 11 апреля 2019

Обновить вас viewDidLoad

 override func viewDidLoad() {
        super.viewDidLoad()

        downloadTeams(completion: { (teams) in

            self.teams = teams

            for index in (0...(self.teams!.count - 1)){
                self.downloadTeamsImage(idTeam: teams[index].getId(), completion:
                    {(teamsImg) in self.teamsImg = teamsImg

                let finalId : Int = teams[index].getId()
                let finalName : String = teams[index].getName()
                let finalMember : Int = teams[index].getMembers()
                var finalURL : URL = URL(string : "http://localhost/MyWebService/images/EmptyImg.png")!

                for index2 in (0...(self.teamsImg!.count - 1)){
                    if (finalId == self.teamsImg![index2].getId()){
                        finalURL = self.teamsImg![index2].getImagePath()
                    }
                }

                let tempCompleteTeam = JsonCompleteTeam(id: finalId, name: finalName, member: finalMember, imgUrl: finalURL)
                self.teamsComplete?.append(tempCompleteTeam)

                // YOU CAN MOVE FORWARD NOW AS ALL THE DATA IS LOADED 
                if index == self.teams!.count - 1 {
                   self.showTeamButton((Any).self)
                }

             }
           })

        })
    }

Это удалит ваш сбой и вызовет showTeamButton, когда все данные будут загружены.

...