Ignoring computed fields in @Selection #96
-
| Hi there, I'm migrating my weight tracking app to SharingGRDB/structured queries. So far, so good, but now I'm facing the following challenge. I would like to keep some computed fields in a selection: Selection
struct WeightPoint
{
    let timestamp: Date
    let meanWeightInKilograms: Double
    @Ephemeral
    let weight: Measurement<UnitMass>
     init(timestamp: Date,
         meanWeightInKilograms: Double        
        )
    {
        self.timestamp = timestamp
        self.meanWeightInKilograms = meanWeightInKilograms
        self.weight = self.weightInLocalUnit(weightInKilograms: self.meanWeightInKilograms)
    }
}However, the  UPDATE: tried to do it with lazy vars but that doesn't compile, because the compiler says I'm trying to mutate an non-mutable object kind regards, Tim | 
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
| Hi @thinkpractice, is  If you really want to do this you can always inline the code that the  | 
Beta Was this translation helpful? Give feedback.
-
| Yeah was wondering the same. It comes back on top of the Instruments traces but may be smth else. So the Ephemeral macro only works on Table then? thanks for the suggestion, will try to implement that tomorrow and see if it’s really the unit conversion or something else… | 
Beta Was this translation helpful? Give feedback.
Hi @thinkpractice, is
weightInLocalUnitreally that expensive? That would be surprising to me if it's just a simple computation to convert units.If you really want to do this you can always inline the code that the
@Selectionmacro writes and modify theinit(decoder:)to computeweightafter the other fields have been decoded.