Hi
I’m having a problem assigning data to an array when fetching a sub collection from firebase. Here’s is my code and the explanation of what’s happening. any help will be appreciate it.
These are my models
import Foundation
import CoreLocation
struct KnownRegions: Identifiable {
var id: String?
var centerLat:Double?
var centerLong:Double?
var radius: Double?
}
struct VisitedRegions: Identifiable {
var visitedCounter: Int = 0
var id: String?
var centerLat:Double?
var centerLong:Double?
var radius: Double?
}
struct UserTrips: Identifiable {
var id:String = ""
var traveledDistance: Double = 0.0
var tripDate: String = ""
var pointsPerTrip: Int = 0
var suddenBreak: [SuddenBreak] = []
var speeding: [Speeding] = [Speeding]()
}
struct SuddenBreak {
// var suddenBrake: Bool?
var streetName: String = ""
var dateSuddenBreake: String = ""
}
struct Speeding: Identifiable {
// var speeding: Bool?
var id: String = ""
var streetName: String = ""
var dateSpeeding: String = ""
}
class User {
var id: String = ""
var name: String = ""
var email: String = ""
var totalPoints: Int = 0
var totalTraveledDistance: Double = 0.0
var visitedRegions: [VisitedRegions] = []
var knownRegions: [KnownRegions] = []
var userTrips: UserTrips = UserTrips()
}
and this is the fetching data method
func getUserTrips() {
guard Auth.auth().currentUser != nil else {
return
}
let db = Firestore.firestore()
let ref = db.collection(Constants.users).document(Auth.auth().currentUser!.uid)
let userTripCollection = ref.collection(Constants.userTripCollections)
userTripCollection.getDocuments { querySnapshot, error in
guard error == nil && querySnapshot != nil else {
return
}
var userTripArrayq = [UserTrips]()
for docs in querySnapshot!.documents {
var u = UserTrips()
var speedingArray = [Speeding]()
u.traveledDistance = docs[Constants.traveledDistance] as! Double
u.pointsPerTrip = docs[Constants.pointsPerTrip] as! Int
u.tripDate = docs[Constants.tripDate] as! String
u.id = docs.documentID
let speedingCollection = userTripCollection.document(u.id).collection(Constants.speeding)
speedingCollection.getDocuments { snapshot, error in
for speedingDocs in snapshot!.documents {
var speeding = Speeding()
speeding.streetName = speedingDocs["streetName"] as! String
speeding.dateSpeeding = speedingDocs["dateSpeeding"] as! String
speeding.id = speedingDocs[speedingDocs.documentID] as? String ?? UUID().uuidString
speedingArray.append(speeding)
}
u.speeding = speedingArray
// if I print this array at this point everything is ok but once it goes out the "for speedingDocs loop" it shows that the array is empty so I can't assign the speeding array subcollection to the userTripsCollection array
}
print(u.speeding) // here it shows that the array is empty
let suddenBreakCollection = userTripCollection.document(u.id).collection(Constants.suddenBreak)
suddenBreakCollection.getDocuments { snapshot, error in
guard error == nil && snapshot != nil else {
return
}
for suddenDocs in snapshot!.documents {
var suddenBreak = SuddenBreak()
suddenBreak.streetName = suddenDocs["streetName"] as! String
suddenBreak.dateSuddenBreake = suddenDocs["dateSuddenBreake"] as! String
u.suddenBreak.append(suddenBreak)
}
}
userTripArrayq.append(u) // here it works perfectly
}
self.userTripsArray = userTripArrayq // here it's ok
}
}
Any help from you guys will be appreciate it