mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-12 04:10:36 +01:00
Strange behaviour of Field.Stored behaving as optional #373
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @ptrkstr on GitHub (Sep 4, 2021).
The documentation shows the following example of Field.Stored
From that, my understanding is if you give a default value or an initial value (via dynamicInitialValue), then the field is non-optional.
So if I have the following model:
Then I should be able to do the following:
However Xcode complains that "Value of optional type 'String?' must be unwrapped to a value of type 'String'" for
nameinperson.name.The very strange thing is that Xcode's code complete shows it as non-optional.

I'm using Xcode 12.5.1 (12E507)
Is this intended behaviour?
I've attached the project.
BugCoreStore.zip
@JohnEstropia commented on GitHub (Sep 4, 2021):
@patrickbdev
ListStates always returnObjectPublisherinstances, and thus there are two things you'd need to note here:person.$nameObjectPublishers, the value of the properties are always an optional of the actual value, because this instance may have been deleted on the thread that it is being accessed on. Think ofObjectPublishers as livingObservableObjects: you cannot just read their values or else your SwiftUIViews will not get automatically updated.Now this is where
@ObjectStates come in. Your innerViewthat needs to read the properties need to use thatObjectPublisherand observe on it through@ObjectState. The@ObjectStatereturns anObjectSnapshotwhich will now let you access.$nameas a non-optional.You can see this all in action in the Demo app, particularly the
Modern.ColorsDemo.SwiftUI.ListView.swiftandModern.ColorsDemo.SwiftUI.ItemView.swiftfiles.@ptrkstr commented on GitHub (Sep 5, 2021):
Thank you for the prompt response @JohnEstropia, that does make sense and answers the question. I'm very impressed with this project, you've gained yourself another sponsor 😇