3

I've got a Firestore Collection I'm querying. I've confirmed that the query is working as expected by printing the contents of the documents its snapshot returns:

Optional([
    "location": <FIRGeoPoint: (44.916159, -93.336624)>,
    "foo": 1,
    "bar": potato,
    "someArray": <__NSArrayM 0x2826b8450>(
        <FIRDocumentReference: 0x2828f58c0>,
        <FIRDocumentReference: 0x2828f5900>
    ),
    "owner": <FIRDocumentReference: 0x2828f5920>,
    "timestamp": 2019-01-05 01:06:37 +0000
])

I am able to successfully get any value from the document except for the timestamp. I'm aware that there are some special behaviors with timestamps, but both of the following give me nil, despite the value clearly being present in the snapshot shown above.

guard let timestamp = documentSnapshot.get("timestamp") as? Timestamp else {
    print("Couldn't get timestamp")
    return
}

guard let timestamp = documentSnapshot.get("timestamp", serverTimestampBehavior: .estimate) as? Timestamp else {
    print("Couldn't get timestamp")
    return
}

For the hell of it, I also tried this, to no avail

guard let timestamp = documentSnapshot.get("timestamp") as? String else {
    print("Couldn't get timestamp")
    return
}

Any idea why I can get every value from the document except for the Timestamp?

(Swift 4.2 with Firebase iOS SDK 5.15.0)

willbattel
  • 1,040
  • 1
  • 10
  • 37
  • The correct type is a `Date`. See https://stackoverflow.com/questions/51116381/convert-firebase-firestore-timestamp-to-date-swift – Frank van Puffelen Jan 05 '19 at 03:22
  • @FrankvanPuffelen hmm... but there is a deprecation warning that says to use the Timestamp object, not Date. It says using Date may cause the app to break in the future. Using Date does indeed work, but the warning says to use `let timestamp: Timestamp = documentSnapshot.get("created_at") as! Timestamp` then `let date: Date = timestamp.dateValue()` – willbattel Jan 05 '19 at 04:34
  • Interesting. I had indeed missed that change. But your code looks fine in that case according to the documentation and Doug's answer here: https://stackoverflow.com/questions/50890640/how-to-parse-firestore-fieldvalue-to-date-in-swift/50890744#50890744. I'm not sure why it would return `nil` in that case. – Frank van Puffelen Jan 05 '19 at 14:17

0 Answers0