0

I created a func that reads from firebase

When I print date is always nil and I locked at firebase in my browser and the date exists. so please can some body help me?

func fetchEventData(group:groupeViewModel)  {
    
    print("featchingEventData")
    if user != nil {
        db.collection("events").whereField("group_id", isEqualTo: group.id).addSnapshotListener({(snapshot, err) in
            guard let documents = snapshot?.documents else {
                print("no docs returned")
                return
            }
                var cont = -1
            self.groupes[group.cont].envents = documents.map({docSnapshot -> eventViewModel in
                        cont += 1
                        let data = docSnapshot.data()
                        let docId = docSnapshot.documentID
                        let name = data["name"] as? String
                        let des = data["des"] as? String
                        let res = data["reservation"] as! Int
                        let cost = data["reservation"] as! Float
                        let isDate = data["isDate"] as! Bool
                        var date = data["date"] as? Date
                        let link = data["link"] as? String
                        let textColor = data["TextColor"] as! Bool
                        var res_users = data["res_users"] as? [String]
                        if res_users == nil{
                            res_users = [""]
                        }
                        if date == nil{
                            
                            date = Date(timeIntervalSince1970: 135670000)
                        }
                print("date \(date)")
                    print("update")
                return eventViewModel(id: docId, name: name!, description: des!, TextColor: textColor, reservation: res, reserved_user:res_users!, cost: cost, isDate: isDate, date:  date!, link: link!, cont: cont)

                })
                print("events \(self.events)")
        })
    }
}
jnpdx
  • 45,847
  • 6
  • 64
  • 94
  • You're trying to cast it to `Date` -- it's probably a `Timestamp` (custom to Firebase) – jnpdx Oct 02 '22 at 22:58
  • ok so I tryed to remove as Date and it prints : Optional() how can I transform this to a int or a Date – Louka Studio Oct 02 '22 at 23:10
  • Use `.dateValue()` https://stackoverflow.com/questions/29243060/trying-to-convert-firebase-timestamp-to-nsdate-in-swift – jnpdx Oct 02 '22 at 23:17

1 Answers1

1

You have to toggle between Swift's Date object and Firestore's Timestamp object. Note that Firestore uses tokens to represent timestamps which is not as straightforward as using primitive types like integers or strings. Therefore, if you see unexpected behavior with these timestamps such as nil where there should be value or values that aren't exactly equal but "close enough", it's likely a side effect of this token system.

Read

if let timestamp = data["date"] as? Timestamp {
    let date = timestamp.dateValue()
}

Write

let now = Date()
let timestanmp = Timestamp(date: now)
trndjc
  • 11,654
  • 3
  • 38
  • 51