View References

I need to edit an attribute of a core data entity fetched in a previous view. So I put var entityName: FetchedResults.Elementin the second view to get access to the entity. But when I do this it doesn't actually get the entity it gets a copy of the entity so when changes are made to it it doesn't actually change. Basically is there a way to pass a reference to an attribute of an object to a struct so that when you set that reference to a value the entity changes?

Any help would be greatly appreciated. All the best!

Replies

I haven’t done this in UIKit, but in SwiftUI I pass the entity to the second view, modify the attribute on that entity, then perform a Context save of the Entity (by using a function in my CodeData stack). CoreData entities are classes and therefore get passed by reference.

Having modified and saved the Entity + attribute, you then probably need to force a view refresh, which can be tricky depending on your implementation of CoreData in your views and whether UIKit or SwiftUI.

regards, Michaela

  • I have a busy morning here in Oz (currently 07:30), but I'll have a look at your code this afternoon - 6 or 7 hours from now. Regards, Michaela

  • I've had a look at your linked post and I started preparing an answer for you, but I implement CoreData solutions differently and I'm not sure that my "quick fix" will actually solve your problem, without recreating you code here and testing - which I won't have time for at the moment. If you don't get an answer from someone else I might be able to look at it again over the weekend.

    Good luck and regards, Michaela

Add a Comment