-1

so I have a fairly complicated scenario in my hands, it goes something like this:

  1. Fetch array of "Repository" entities
  2. Parse the repository array
  3. For each repository entity, the branches need to be fetched
  4. Once branches have been fetched, modify the original repositories, by attaching the corresponding branches to each
  5. Return final object with all their properties

So the code is something like this:

    func fetchRepos(token: Token) -> Promise<[Repo]> {
        return Promise { seal in
            guard let url = URL([SOME_URL])...

            var request = URLRequest(url: url)
            request.addValue(token.token, forHTTPHeaderField: "X-API-Token")
            request.addValue("application/json", forHTTPHeaderField: "accept")

            URLSession.shared.dataTask(with: request) { (data, _, error) in
                if error == nil {
                    do {
                        let reposDto = try self.decoder.decode([RepoDto].self, from: data!)

                        var repos = reposDto.map { dto in DtoMapper.mapRepoDto(dto, token: token) }

                        firstly {
                            when(resolved: repos.map { self.fetchRepoBranches(token: token, repo: $0) })
                        }.done { branchMatrix in

                            for (repo, result) in zip(repos, branchMatrix) {
                                var repo = repo
                                switch result {
                                case .fulfilled(let branches):
                                    repo.branches = branches // THIS STEP IS NOT WORKING
                                    repo.owner = "TEST" // ALSO DOES NOT WORK
                                    print("inserting branches into repo", repo, branches)
                                case .rejected:
                                    print("Repo branches could not be inserted \(repo.name)")
                                }
                            }
                            print("Repos have been fulfilled", repos)
                            seal.fulfill(repos)
                        }
                    } catch {
                        print("Error parsing Repos")
                        seal.reject(error)
                    }
                } else {
                    seal.reject(error!)
                }
            }.resume()
        }
    }

So all the code compiles and runs, but when I try to assign the branches the repo object does not get mutated, the final print, still prints the original array of objects without any of the inserted/updated properties, what am I doing wrong here?

Oscar Franco
  • 5,691
  • 5
  • 34
  • 56

1 Answers1

1

For the life of me I could not find how to mutate the original repo array, so I went with the flow and simply created a new array (since zip creates a new copy):

                  firstly {
                            when(resolved: repos.map { self.fetchBranch(token: token, repo: $0) })
                        }.done { branchMatrix in
                            var newRepos = [Repo]()
                            for (repo, result) in zip(repos, branchMatrix) {
                                var repo = repo
                                switch result {
                                case .fulfilled(let branches):
                                    repo.branches = branches
                                    newRepos.append(repo)
                                case .rejected:
                                    print("Repo branches could not be inserted")
                                }
                            }

                            seal.fulfill(newRepos)
Oscar Franco
  • 5,691
  • 5
  • 34
  • 56