so I have a fairly complicated scenario in my hands, it goes something like this:
- Fetch array of "Repository" entities
- Parse the repository array
- For each repository entity, the branches need to be fetched
- Once branches have been fetched, modify the original repositories, by attaching the corresponding branches to each
- 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?