Skip to content

Calendar displaying same value every day #1403

@SosaFrancys

Description

@SosaFrancys

(Required) Version Number:
7.1

Description

I'm trying to update my calendar to get the data needed from Firestore instead of CoreData which is working fine but when I try to display the data from Firestore the whole calendar gets the same value

Steps To Reproduce

Function in my User Struct
` static func getUserHoursFromFirestore(date: Date, from snapshot: DocumentSnapshot, forField field: String, completion: @escaping (Double?) -> Void) {

    let dayFormatter = DateFormatter()
    dayFormatter.dateFormat = "MM-dd-yyyy hh:mm a"
    let documentID = dayFormatter.string(from: date)
    
    let db = Firestore.firestore()
    let user = Auth.auth().currentUser
    guard let userEmail = user?.email else {
        print("++ User is not logged in")
        completion(nil)
        return
    }
    
    let userCollection = db.collection("users").document(userEmail)
    let hourCollection = userCollection.collection("hours").document("Nov 25 2024 05:00 PM")
    
    hourCollection.getDocument { document, error in
        if let error = error {
            print("++ Error fetching document: \(error.localizedDescription)")
            completion(nil)
            return
        }
        guard let document = document, document.exists else {
            print("++ Document does not exist")
            completion(nil)
            return
        }
        
        if let hours = document.get(field) as? Double {
            print("++ Retrieved hours: \(hours)")
            completion(hours)
        } else {
            print("++ Field does not exist or is not a Double")
            completion(nil)
        }
    }
}       `

my handleFirestoreHours on cellForItemAt

`func handleFirestoreHours(cell: CalendarCell, cellState: CellState) {
let db = Firestore.firestore()
let user = Auth.auth().currentUser
let userCollection = db.collection("users").document((user?.email)!)
let hourCollection = userCollection.collection("hours").document("Nov 25 2024 05:00 PM")

    hourCollection.getDocument { snapshot, error in
        
        if let error = error {
            print(error.localizedDescription)
        }else if let snapshot = snapshot {

            User.getUserHoursFromFirestore(date: cellState.date, from: snapshot, forField: "totalHours") { hours in

                print("** THE Number OF hours in handle is : \(String(describing: hours!))")
                if hours == 0.0 {
                cell.hoursLabel.isHidden  = true
            }else{
                cell.hoursLabel.text = "\(hours!)Hrs."
                cell.hoursLabel.isHidden = false
            }
        }
        
    }
    }   
    } `

Expected Behavior

the calendar should be displaying only the hours for Nov 25, 2024, for testing purposes then the documentID will be used

Additional Context

Since the app is working fine using core data at version 7.1 I haven't tried updating to version 8.0.5
I'm fairly new to swift and I hope you can help me solve this issue, thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions