Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct the description of when you can access and mutate state prope… #4

Merged

Conversation

ice-cream-coder
Copy link
Contributor

…rties to align with what's in Apple's official documentation.

This is what it says in Apples documentation for @frozen @propertyWrapper struct State<Value>

You should only access a state property from inside the view’s body, or from methods called by it. For this reason, declare your state properties as private, to prevent clients of your view from accessing them. It is safe to mutate state properties from any thread.

I tried to rephrase the wording in Apples documentation as not to plagiarize, but I since Apple's documentation is already very concise, I feel like I just added fluff to something that was already clear. I'm open to suggestions regarding rephrasing.

This edit should clarify two points:

  1. Accessing a state property is actually more strict than using the main thread. It should only be accessed from the call to the view's body.
  2. Modifying state is actually completely permissive and can be accessed from any thread.

…rties to align with what's in Apple's official documentation.
blsage
blsage previously approved these changes Feb 19, 2021
/// - All modifications to a state variable **must** happen on the main thread. Modifying a state variable on a background thread may lead to undefined behavior.
/// - Accessing a state property is only valid when done form inside the view's body property or code called from the view's body property.
/// - Because state properties should only be accessed from the body it's good practice to declare them as private.
/// - Although you should only access the state from inside the body, you can mutate it elsewhere including from any thread.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this is a good change – it's important content that's missing. Thanks for the PR. In order to maintain grammatical consistency with the rest of the docs, consider modifying the above to:

  • Only access a state property from inside the view’s body, or from methods called by it.
  • Apple recommends users to declare state properties as private, to prevent clients of your view from accessing them.
  • All modifications to a state variable must happen on the main thread. Modifying a state variable on a background thread may lead to undefined behavior.

I'm honestly unsure what Apple means by "it is safe to mutate state properties from any thread." @vmanot any ideas?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think they mean that you can mutate a state property from any thread; e.g. any background thread, main thread, serial thread, concurrent thread. Do you have any evidence that that isn't correct. In my own project I modify the state from a network callback without switching back to the main thread and haven't come across any issues.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I suppose I got confused, because I've gotten errors around "modifying State during view update", as discussed here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the difference is tbh. Is "modifying State during view update" not doing things on a background thread? What's the difference?

Copy link
Contributor Author

@ice-cream-coder ice-cream-coder Feb 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modifying state would happen if you assign a state variable during the body call. I've only done this on accident trying to write UIViewRepresentable code, but here would be an minimum code example of what you can't do:

struct MyView: View {
  @State private var state = 0
  var body: some View {
     text
  }
  var text: some View {
    state = 2 // <--------- This is an example of modifying state during a view update and leads to undefined behavior.
    return Text("\(state)")
  }

I had the same confusion and just figured out that you can modify state on other threads a few days ago when I was looking at the Apple documentation. That's why I jump on updating this.

@ice-cream-coder
Copy link
Contributor Author

@AlexFine I took your suggestion for bullet point one, and with some modifications bullet point two. I can't take your suggestion for bullet point three because to my best understanding it's not correct. I think the point is that you can modify State properties anywhere at anytime and under the hood SwiftUI will make sure that when body is called, the value is set correctly, but if you access a state property outside of body, then the result is undefined.

Copy link
Contributor

@AlexFine AlexFine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving this PR to be merged in. We should add another line on State that relates to the concerns mentioned here: https://stackoverflow.com/questions/57340970/modifying-state-during-view-update-this-will-cause-undefined-behavior-error

But overall this PR is good & the content should be added.

@AlexFine AlexFine merged commit d3cb631 into SwiftOnTap:main Feb 19, 2021
denisenepraunig pushed a commit to denisenepraunig/Docs that referenced this pull request Apr 17, 2021
Update akf documentation branch
denisenepraunig pushed a commit to denisenepraunig/Docs that referenced this pull request Apr 17, 2021
…sDescription

Correct the description of when you can access and mutate state prope…
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants