As part of trying to optimise time spent on large queries (fetch + decode), i am trying to find the fastest solution to decode results.
At the moment, the fastest way i found by my benchmarking that works reliably is manually mapping the values inside a result to my local object. While this adds a good chunk of boilerplate, going through .toJSON()
, then serialising the JSON and then decoding it is much slower in my tests.
However, there is another API in the CouchbaseLiteSwift package both on Result and ResultsSet (.data(as: _)
) that I would like to test but that seems to break for dates.
So my questions are:
- Is the
.data(as: _)
API broken or am I doing something wrong? - What is the fastest supported way to decode a result, regardless of how much code it implies?
Thank you!
Example for reference:
struct Foo: Codable {
let id: String
let name: String
let someDate: Date?
// ALWAYS FAILS
static func fromData(_ result: Result) throws -> Foo {
try result.data(as: Foo.self)
}
// WORKS RELIABLY (fastest decode strategy i found thus far)
static func fromFields(result: Result) throws -> Foo {
guard
let id = result.string(forKey: "id"),
let name = result.string(forKey: "name")
else {
throw NSError()
}
let someDate = result.date(forKey: "someDate")
return Foo(
id: id,
name: name,
someDate: someDate
)
}
}
1 post - 1 participant