|
I'm trying to reorganize my code as suggested in https://www.hackingwithswift.com/books/ios-swiftui/introducing-mvvm-into-your-swiftui-project. So I'm dealing with moving all my Core Data methods (save, delete, update...) on a separate file to call that methods from the correspondent views. I've tried creating functions taking parameters to be used to write data on CD. I instantiate there as well an instance of my DataController in order to get to my ManagedObjectContext:
But when trying to call this method, for example, from the view that takes this data, I don't find the way to invoke that method properly. Have I to instantiate this class in order to use its methods?
But it doesn't work:
Is this the correct way to do it? Or maybe with an extension of "Student" entity? I've tried as well, without success. How then? |
|
More details: I've been able to invoke this method. The problem was that I was trying from inside a confirmation dialog button this way:
Now, with this sintax, it works:
BUT. Now compiler says "An NSManagedObjectContext cannot delete objects in other contexts.". Yes: I'm trying to access the same context I use all along my app:
What am I missing? |
|
Answering myself 😅. Just in case it's usefull for somebody: I've passed as well, as a parameter, the context:
Now, it's working: the "environment" context is added on my app to MainView itself, so this class has no access to it. In adittion: doing this way, it's possible to create, even better than a Class, an Extension of Student, with exactly the same estrategy, and using simply (wihtout having to instantiate nothing at all):
🙃 |
|
hi Óscar, i use extensions on all Core Data objects to do simple things, such as nil-coalesce text or date fields, or to convert Int16 and Int values (so SwiftUI views need not have to do that), and i sometimes use a similar delete strategy ... i think that's a darn good idea. although when you write
you don't have to over-do things. for example, you don't have to ask a
now you can simply ask a student to delete itself with hope that helps, DMG |
|
Thanks @delawaremathguy 😊. I tried that way, but accesing the context was a problem. The way you give is more clear, and work perfectly. But... how can I use the same strategy for saving? This code on Student extension:
forces me to unwrap "moc" (as it's optional, as you say). I can't force (!) or coalesce (??) it, as it's a new object and does not have a context already. I have my context "injected" on environment, but as this is an extension and not a view, I can't access the usual way:
|
|
For saving the same approach should work:
But you can just save from your dataController as well. But this is not a valid approach for creating a new Student. This would mean that an already existing Student creates a new Student which probably isn't what you want. You can write a public static function on your Student extension to create a new Student. You then have to pass the moc into the function, though.
You then can create a new Student with:
I don't see much benefit over
Either way, for creating a new Student you have to pass the NSManagedObjectContext from an external source. In your example the already existing Student would be this external source. The static function could be a good choice if you want to set specific parameters everytime you create a Student. So you don't have to write them everytime you create a Student. |
|
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.