|
I'm trying to instantiate a "Student" object to be shown on my editionView, over a modal sheet, like this:
It works: sheet is presented, with the editionView prepared to add data, but I see on the console the message I've written on the title of this post. I've tried to "force" this new object to be instantiated on the specific Core Data context:
But doing so results on instantiating "infinite objects" (or that's the result presented on screen). Any suggestions to make this console error dissapear? |
|
I'm not sure if I understood you correctly? With your sheet you want to add a new Student and not edit an existing one? If it's the first one you can create the new Student object in the init method of the sheet itself. With sheets I think the problem is that you still have to add the moc as environmentObject.
Your problem with the "infinite" objects is probably the result of creating a new instance of Student everytime you open the modal but you don't discard it if you don't want to keep it. So in your modal you have to discard the changes (Rollback) if you just dismiss the sheet and you should add an explicit "Save"-Button to save the changes and keep the Student and then dismiss the sheet. |
|
In fact, I am using that editView both for my students to be edited and to be added, depending on an "isEditing" property to control what to do in each case.
About "infinit students" result, it't not exactly the way you say. If I pass the context on editing call:
The result is as seen on the attached image. And with this console message: ForEach<FetchedResults<Student>, Optional<UUID>, ModifiedContent<ModifiedContent<NavigationLink<VStack<TupleView<(StudentCellView, ModifiedContent<ModifiedContent<Rectangle, _FrameLayout>, _EnvironmentKeyWritingModifier<Optional<Color>>>)>>, StudentDetailView>, _PaddingLayout>, ContextMenuModifier<ZStack<ModifiedContent<Button<_ConditionalContent<Label<Text, Image>, Label<Text, Image>>>, StyleContextWriter<MenuStyleContext>>>>>>: the ID nil occurs multiple times within the collection, this will give undefined results! I cancel the adittion, but the list is that of the image, altough when quitting app and open it again all dissapear. It seems that if I instantiate a new student just on editView opening, the forEach responsible for presenting the list with all of them (behind the modal editView) is adding one after another "new", infinitely. So maybe the best approach would be not passing any student at all when creating a new object. But @ObservedObject can't be optional 🥴 |
|
Yes, but your
with this approach your isEditing may not be needed, either. But I'm sure you would need to pass Your second case never works because you can't create a |
|
But then, where I declare my @ObservedObject property? This throw errors:
Variable 'self.student' used before being initialized. Return from initializer without initializing all stored properties. Here, the beginig of editView:
|
|
Gna, this s?cks. Problem is that you can't access moc in the initializer. The explanation is here. My apologies. I forgot about that. This is also the explanation of your phantom Students. You could pass moc in the init as well. Then you don't need the environment. I don't have a better solution.
|
|
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.
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.