Check this: https://developer.apple.com/forums/thread/716156
I had the same problem. The solution shown worked for me. Don't love it, seems hacky, there must be a better way, BUT: it works.
Post
Replies
Boosts
Views
Activity
Don Nissen, you the person! It actually works! Thank you, thank you, thank you. I was failing bad, and trying to use a dynamic fetch request thing I saw on the invaluable Hacking with Swift, but I wasn't happy with the solution, since it necessitated refetching every time a reordering is called, which seems wasteful.
Am I correct in parsing what you do as transferring the core data entity (whatever thing that is) into an array (which is what I hoped a core data entity should be)?
The only sad I have is that I need to make a second file to achieve that. Tried to do everything in the same file unsuccessfully.
Thanks again. I haven't seen anyone with a solution to this. I don't understand how it's not a more prevalent problem for more people. I even did an "Ask Apple" office hour appointment, and their solution was to refetch, which I don't see how that is not hacky within a table.
Anyways, thanks Don Nissen.
I have still not found a solution for this. I don't see how to do another fetch request within the table, plus it seems wasteful in this case (no data would have changed in my context).
Fixed it. Put the func inside the main view (save view) struct, so I didn't need to pass the FocusState as a parameter. Put a switch statement (isn't there a better way to cycle through enums?) to get desired behavior. Code is pukeful looking, but: it works!
Sorry, that’s on me. I meant that if you try tapping the button, but accidentally slightly move your finger as you tap, it won’t register it as a swipe. BabyJ solution below works. Yay, BabyJ!
Dude, thank you SO much! You saved me days of floundering cluelessly.
I did have PersistenceController injected into ContentView exactly as you describe. For some reason, in the ViewModel I needed to make the "p" capital as in "moc = PersistenceController.shared.container.viewContext". I'm only mentioning this to help another clueless soul like me that might end up looking at this thread.
You made my code way less emetic, and I thank you again for that.
a.
Thanks Zimmie, DMG. It did work after adding the NSSecureUnarchiveFromData to the transformer field.
(Shouldn't Xcode have a dropdown with the built-in custom-classes in there? Not very discoverable, otherwise.)
Zimmie, DMG: I have a follow-up if you even happen to see this. I don't think I need to upload the code to ask the question. Let me try.
my try? moc.save() statements (I did change them to do/catch eventually while trying to debug) were in a non-view file where the class dealing with the data that needed to be saved lived, and was transferred to core data through a method part of that class. The class had "@Environment(.managedObjectContext) var moc" in there, and the app compiled fine.
The app did compile, but the data wouldn't save in core data. Eventually I figured out it that the try save.moc spat the error "The operation couldn’t be completed. (Foundation._GenericObjCError error 0.)". Googling that wasn't too productive either, but something I saw made me try to put all of the method code into the appropriate button within the view that called for that method (it's quite a bit of code). That, somehow, worked.
My question: why? It does work, but my code looks even uglier than my usual *****. I would love to move that to the class in a separate file. Thanks a ton.