I'm creating a customer management app for iOS. I'm using Firebase to store data and files. My app have multiple view, in main view are the customers and the second for saving documents and display and the third is where you can add some callDetails what you speak last time with this customer.
So far I can save data in Firestore and files in storage and display them.
My problem is how can I structure the database that each document point to the right customer and same for callDetails. I know that I need to add references in the customer collection like Nested Data for example but I'm stuck with this. So how can I save a reference to customer collection and then fetch the data?
I refer to this article but no luck: https://peterfriese.dev/posts/firestore-codable-the-comprehensive-guide/
CustomerStruct.swift
struct Customers: Identifiable, Codable {
@DocumentID var id: String?
var fullName: String
var phoneNum: String
var email: String
var address: String
var profession: String
var age: String
var dateOfBirth: String
enum CodingKyes: String, CodingKey {
case id
case fullName
case phoneNum
case email
case address
case profession
case age
case dateOfBirth
}
}
LastCallStruct.swift
struct LastCallData: Identifiable, Codable {
@DocumentID var id: String?
var title: String
var description: String
var date: String
enum CodingKyes: String, CodingKey {
//case id
case title
case description
case date
}
}
this is how i save the customers data the same is for callDetails
let newCustomers = Customers(
fullName: nameTextField.text!,
phoneNum: phoneTextfiled.text!,
email: emailTextField.text!,
address: addressTextField.text!,
profession: professionTextField.text!,
age: ageTextField.text!,
dateOfBirth: dateOfBirthTexField.text!)
do {
try self.db.collection("...Customers").addDocument(from: newCustomers)
} catch let error {
print("Error writing data to firestore,\(error)")
}
This is how I fetch customers and the same for callDetails
func loadCustomers(){
db.collection(self.collectionName).addSnapshotListener { (QuerySnapshot, error) in
self.customerArray = []
if let error = error{
print("Error geting documents,\(error)")
} else {
for document in QuerySnapshot!.documents{
print("\(document.documentID) => \(document.data())")
let data = document.data()
if let customerName = data["fullName"] as? String, let customerNumber =
data["phoneNum"] as? String {
let newCustomer = Customers(id: document.documentID,fullName: customerName, phoneNum: customerNumber, email: "", address: "", profession: "", age: "", dateOfBirth: "")
self.customerArray.append(newCustomer)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
}
}
This is how i save document data in storage and put a reference in a new collection but i want this to point to customer collection
private func handleFileSelection(inURL:URL) -> Void {
// let filePath = inURL.deletingPathExtension().lastPathComponent
filePath = inURL.pathComponents.last!
// file Storage reference path
let path = "customer name/\(filePath)"
let files = storage.reference().child(path)
do {
let data = try Data(contentsOf: inURL)//Geting file here
//Upload the data for the specific file in firebase
files.putData(data, metadata: nil) { (metadata, error) in
//Save a reference into firestore
self.db.collection("Documents").document().updateData(["url": path])
guard metadata != nil else {
print("Error while uploading data, \(String(describing: error))")
return
}
files.downloadURL { (url, error) in
//Download the file URL
let urlString = url?.absoluteString
print("Dowloaded URL: \(String(describing: urlString))")
}
print("Success")
}
} catch {
print("Document loading error")
}
}
This is how my database looks like