@adamdt2007
Hi Adam,
This did my head in for a while but I finally figured it out.
One thing you have to be really careful about is the naming convention you use with your structs (or classes for that matter) to avoid using a Swift reserved word. For example the struct naming of Location and Coordinate are both from built in API’s that use those two names. Whilst this might work in this scenario there is always a chance that the complier will get confused. So what I did as part of this solution is to rename those two to be more explicit.
struct JobDetail: Identifiable, Decodable, Encodable {
var id: String
var company: String
var jobName: String
var jobAddress: JobLocation
var jobLocation: JobCoordinate
var jobImageURL: [String]
var jobCreationDate: String
var jobCreatedBy: String
var active: Bool
var jobPM: String
// var jobStaff: [PersonDetail]?
//
// var reports: [ReportDetail]?
}
struct JobLocation: Decodable, Encodable {
var address: String
var city: String
var country: String
var state: String
var zipCode: String
}
struct JobCoordinate: Decodable, Encodable {
var latitude: String
var longitude: String
}
Note that I commented out the jobStaff
and reports
attributes as that was causing errors for me since there was nothing in your Firebase code to provide data for ether.
and for the getFirebaseData function, the code changes to this:
func getFirebaseData() async -> [JobDetail] {
//set up Firestore basics
let db = Firestore.firestore()
let collection = db.collection("JobDetial")
do{
let querySnapshot = try await collection.getDocuments()
let allJobs = querySnapshot.documents.map { document in
return JobDetail(id: document.documentID,
company: document["company"] as? String ?? "",
jobName: document["jobName"] as? String ?? "",
jobAddress: JobLocation(address: document["address"] as? String ?? "",
city: document["city"] as? String ?? "",
country: document["country"] as? String ?? "",
state: document["state"] as? String ?? "",
zipCode: document["zipCode"] as? String ?? ""),
jobLocation: JobCoordinate(latitude: document["latitude"] as? String ?? "",
longitude: document["longitude"] as? String ?? ""),
jobImageURL: document["jobImageURL"] as? [String] ?? [""],
jobCreationDate: document["jobCreationDate"] as? String ?? "",
jobCreatedBy: document["jobCreatedBy"] as? String ?? "",
active: document["active"] as? Bool ?? true,
jobPM: document["jobPM"] as? String ?? ""
)
}
return allJobs
} catch {
print("Error getting documents: \(error)")
}
return [JobDetail] ()
}
For future posts, when you want to add a code block from Xcode the three backtick characters preceding the code should look like this:
```
They are the backwards facing character on the same key as the tilde ~ (at least that is the case on my QWERTY keyboard here in Australia).
The other way to get a code block in place is to get a fresh line in the editing window and then tap on the </> button at the top and you will get a block with the 3 backticks above and below with the words “type or paste code here” in between so you simply paste your code in place of the those words.
Hope that helps you out.
Firebase is tricky but when you master (and I don’t claim to be at that level yet) it I think it’s a great solution for iOS Apps.
Cheers
Chris P