Reputation: 53
After running my app I get the following output in the terminal. The app does not crash. Where exactly should I put this code? And what exactly does it do?
2018-08-16 09:45:05.414410-0400 Yubi[2652:1608362] 4.13.0 - [Firebase/Firestore][I-FST000001] The behavior for system Date objects stored in Firestore is going to change AND YOUR APP MAY BREAK. To hide this warning and ensure your app does not break, you need to add the following code to your app before calling any other Cloud Firestore methods:
let db = Firestore.firestore() let settings = db.settings settings.areTimestampsInSnapshotsEnabled = true db.settings = settings
With this change, timestamps stored in Cloud Firestore will be read back as Firebase Timestamp objects instead of as system Date objects. So you will also need to update code expecting a Date to instead expect a Timestamp. For example:
// old: let date: Date = documentSnapshot.get("created_at") as! Date // new: let timestamp: Timestamp = documentSnapshot.get("created_at") as! Timestamp let date: Date = timestamp.dateValue()
Please audit all existing usages of Date when you enable the new behavior. In a future release, the behavior will be changed to the new behavior, so if you do not follow these steps, YOUR APP MAY BREAK.
Upvotes: 3
Views: 2591
Reputation: 2916
You should add this in appDelegate, where you are adding the configuration.
FirebaseApp.configure()
let db = Firestore.firestore()
let settings = db.settings
settings.timestampsInSnapshotsEnabled = true
db.settings = settings
By setting areTimestampsInSnapshotsEnabled to true, it will save the timestamps instead of Date
So, when you read it will return the timestamps, so wherever in your code, there was Date being read, it should now read timestamp and convert it to Date objects as suggested.
Old code
// old: This is old code returning Date object
let date: Date = documentSnapshot.get("created_at") as! Date
Should be replace by new code
// new: This is new code returning Timestamp object
let timestamp: Timestamp = documentSnapshot.get("created_at") as! Timestamp
let date: Date = timestamp.dateValue()
Hope it helps you to understand the warning. You must make the changes to your existing code to meet the new guidelines, or later it will leads to crash due to type mismatch.
Upvotes: 9