I have two CoreData entities A and B, there is a one to one relationship between them. B entity records must always exist. Also A must have a relationship to B.
However, CoreData enforces optional relationship.
ForEach(items, id: \.self) { item in
MyView(aItem: item, bItem: item.B)
}
Therefore, item.B
above is an optional.
I need the bItem
value to be an ObservedObject, however they can not be optionals
struct MyView: View {
@ObservedObject var aItem: A
@ObservedObject var bItem: B
I've tried wrapping an if let
around MyView, but this cause a NavigationLink pop issue.
Which ever way I turn I face problems with the optional.
CodePudding user response:
You would deal with it here
MyView(aItem: item, bItem: item.B ?? BManager.create(itemA: item))
BManager
being a Manager object that would hold a convenience method to create a blank object.
That way if B
happens to be nil
an object would get created immediately.
Here is something similar it involves a struct
<->CoreData relationship so it isn't exact but similar to this.