Devlog: Nov 2, 2022

Spent some time making my Blot blog look more like my current blog. I think it turned out nicely. I couldn’t get the Blot local environment running, and seems that others have the same issue right now. Also couldn’t get local theme to sync, so I did everything on the online editor. It was a bit of a pain, but I managed.


Got back into Lightbulbs, my WIP iOS app for collecting ideas. Was having a crash when adding a collection. I’m converting a FetchedResults to an Array as the data source for the view causing the issue. The issue was that the new collection was not in that array at the time a onChange was called. So I added another onChange to check for when the array changes and run the logic to set the variable again with the new values. A little weird. The closure gets passed the new value as expected, but the variable in the view is still the previous value. But I don’t think that’s how it’s supposed to work. The docs mention capturing the previous value:

.onChange(of: playState) { [playState] newState in
    model.playStateDidChange(from: playState, to: newState)
}

It seems that this is a result of passing the state into another View:

struct ContentView: View {
    @State private var value = 1
    
    var body: some View {
        MyLabel(value: value)
        Button("Increment value") {
            value += 1
        }
    }
}

struct MyLabel: View {
    let value: Int
    
    var body: some View {
        Text(value.formatted())
            .onChange(of: value) { newValue in
                print(value, newValue)
            }
    }
}

Maybe there’s a better way to do this. Need to look into state management again. But at least in this situation, it seems to be the case that when onChange gets called in MyLabel, value is always the old value.



Date
November 2, 2022