I am a bit confused why I am getting this error thrown while trying to read from the database. In my test, I write the record successfully. Immediately after the write, I am trying to read the records in the database and have the error "decoding a single value container is not supported".
The Codable object is this:
struct AddressTypeTwo: Codable {
// MARK: CRUD
var id: Int?
var created: Date?
var createdBy: String?
var modified: Date?
var modifiedBy: String?
var deleted: Date?
var deletedBy: String?
var name: String?
var companyId: Int?
// MARK: CodingKeys
enum CodingKeys: String, CodingKey {
case id, created, createdBy, modified, modifiedBy
case deleted, deletedBy
case name
case companyId = "company_id"
}
}
The insert works fine:
func insert(toBeInserted obj: Codable) -> Int {
if let newId = try? DbSupport.shared.activeDatabase?.run(table.insert(obj)) {
return Int(newId)
}
return -1
}
I am using this function to retrieve all records - and this is where I get the error:
func get<T: Codable>(_ includeDeleted: Bool = false) -> [T]? {
var query = table.filter(tableDeleted == nil)
if includeDeleted {
query = table
}
guard let database = DbSupport.shared.activeDatabase else { return nil }
// these always come back in an array even though there is an id
let addresses: [T?]? = try? database.prepare(query).map { row in
if let rowData: T = try? row.decode() {
return rowData
}
return nil
}
return addresses?.compactMap { $0 }
}
Try changing your DATETIME fields to TEXT. SQLite.swift maps Date objects to Text