Badr Bujbara
Badr Bujbara

Reputation: 8691

Firebase iOS Swift: Cannot store object of type FIRTimestamp at

I have an item object that I'd like to store on firebase, but the date timestamp isn't letting that happen.

Here is my Item struct:

    struct Item{
    var id: String = ""
    var itemName: String = ""
    var imageUrl: String = ""
    var price: String = ""
    var description = ""
    var categoryId = ""
    var postedBy = ""
    var postedByName = ""
    var coordinate = ""
    var date = Timestamp()


    func toDictionary() -> [String: Any]{
        return [ItemFields.ID: id,
                ItemFields.ITEM_NAME: itemName,
                ItemFields.IMAGE_URL: imageUrl,
                ItemFields.PRICE: price,
                ItemFields.DESCRIPTION: description,
                ItemFields.CATEGORY_ID: categoryId,
                ItemFields.POSTED_BY: postedBy,
                ItemFields.POSTED_BY_NAME: postedByName,
                ItemFields.COORDINATE: coordinate, 
                ItemFields.DATE: date
        ]
    }
}

When saving the item

 ref.setValue(item.toDictionary, withCompletionBlock: {(err, ref) in

I get this error:

Cannot store object of type FIRTimestamp at date. Can only store objects of type NSNumber, NSString, NSDictionary, and NSArra

Upvotes: 0

Views: 274

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317692

Firebase offers two databases, Realtime Database and Firestore. You didn't say which database you're writing to, but the fact that you're calling setValue() suggests that you're using Realtime Database.

Realtime Database doesn't have Timestamp type data. Timestamp is a Firestore concept, and the error message is telling you that you can't write a Timestamp type object to Realtime Database. If you want to store a moment in time to Realtime Database, you should store a standard unix time in milliseconds represented as a 64 bit integer.

Upvotes: 2

Related Questions