in my ObservableObject I get data from a API and load this data in an array called shows. This works totally fine. Later on I want to modify this array with Data from Firebase(Code below). You can see in the Code comments, where I've written, which part is not executed. Can someone tell me why these function do not get executed. Thanks in advance
class observer : ObservableObject{
@Published var shows = [stacks]()
@Published var last = -1
var results = [Result1]()
init(){
let number = Int.random(in: 1...35)
let endnumber = number + 8
for n in number...endnumber{
guard let url = URL(string:"https:..." ) else {
print("Invalid URL")
return
}
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
DispatchQueue.main.async {
self.results = decodedResponse.results
for i in self.results{
self.shows.append(stacks(id: "\(i.id)", name: i.name, typ: "Serie", status: "",overview: i.overview, vote: "\(i.vote_average)", image: i.poster_path, swipe: 0, degree: 0, commercal: "no"))
}
self.shows.shuffle()
}
return
}
}
print("Fetch failed: \(error?.localizedDescription ?? "Unknown error")")
}.resume()
}
// Here I want to count my array, and print the number of items.
// It just print("The amount of shows is: 0"), which is not true
let count = self.shows.count
print("The amount of shows is: \(count)")
// Here I want to modify the array "shows" with Data from firebase. This
// doesn't work either. It is not because of the Code itself. It works in
// a separate function totally fine. The Code doesn't get executed at all
// at this position.
for i in 0..<self.shows.count{{
let db = Firestore.firestore()
let user3 = Auth.auth().currentUser?.email!
db.collection(user3!).getDocuments {(snap, err) in
if err != nil{
print((err?.localizedDescription)!)
return
}
for j in snap!.documents{
let name3 = j.get("name") as! String
if name3 == self.shows[i].name{
let status = j.get("status") as! String
self.shows[i].status = "gesperrt"
}
}
}
}
}
}