0

I'm not sure what to do here, previous errors told me to make this struct encodable but I'm not sure how exactly how to do it? ChatMessage works but ImageMessage does not. The specific error is Type 'ImageMessage' does not conform to protocol 'Encodable'

import Foundation
import FirebaseFirestoreSwift
import UIKit

struct ChatMessage: Codable, Identifiable {
    @DocumentID var id: String?
    let fromId, toId, text: String
    let timestamp: Date
}

struct ImageMessage: Encodable, Identifiable {
    @DocumentID var id: String?
    let fromId, toId: String
    let image: UIImage
    let timestamp: Date
}```
vadian
  • 274,689
  • 30
  • 353
  • 361
  • `UIImage` is not codable type, check this out https://stackoverflow.com/questions/46197785/how-to-conform-uiimage-to-codable – Quang Hà Mar 18 '22 at 02:22

1 Answers1

1

As mentioned by Quang Ha, UIImage is not a codable type. Even if you manage to make it codable I doubt firebase can store an image. You can try to parse the image into a huge array and store it that way.

But I see a massive problem here: you will experience crazy overhead as firestore is not made to store large data. I encountered a similar problem before. My suggestion is to not store the image directly but to store an identifier of that image inside firestore. The image will be stored in the firebase storage, which can be retrieved given an image id.

Yuanda Liu
  • 150
  • 1
  • 4