Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Fixed stored @State properties on App #34

Merged
merged 3 commits into from
Jun 11, 2024

Conversation

Amzd
Copy link
Contributor

@Amzd Amzd commented Jun 1, 2024

Steps

  • Add your name or username and a link to your GitHub profile into the Contributors.md file.
  • Build the project on your machine. If it does not compile, fix the errors.
  • Describe the purpose and approach of your pull request below.
  • Submit the pull request. Thank you very much for your contribution!

Purpose

Currently when using a State property with storage on App instead of on a View, the app ID was accessed before it was set, causing the State internals to look for the data in the folder temporary/ which usually doesn't exist because at the time you will be writing into the State the app ID will be set and it writes the value into app.id/ folder.

This happened because when the State property is on App it is initialized before GTUIApp.appID is set.

struct ExampleApp: App {
    let id = "me.amzd.Example"
    var app: GTUIApp!

    /// This State will try to read its value for the key example from disk before we can access `id`
    /// (because we need to initialize ExampleApp to access the id property)
    @State("example") var example: String?
    
    // ...
}

Approach

I fixed this by making the App.id property static and forwarding it to GTUIApp.appID first thing the program does. I have also made the GTUIApp.appID force unwrapped optional so regression of this issue is not possible.

I am open to suggestions of how to fix this differently. You can also fix this yourself in a different way without crediting, it is not that complicated.

Since it is a breaking change I still need to update the examples

@david-swift
Copy link
Member

Thanks for opening the dicussion and already providing a potential solution! I prefer not to require the user to add a static variable, so I'll see whether I can make the state variables load the files after initializing the app structure. Otherwise, I'll merge your implementation.

Read the file's value as soon as the state value is accessed for the
first time
@david-swift
Copy link
Member

Instead of reading the app id at the initialization of a state variable, it will now read it as soon as the value is accessed for the first time. That way, the id property can stay on an app's instance.

@david-swift david-swift merged commit 1723fcd into AparokshaUI:main Jun 11, 2024
1 check passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants