|
Hello there, I'm very very new to swift, swift UI (only 10 days new !) and I'm stuck on something. I have a many to many relationship with SymptomesEntity and TraitementEntity (both just have a name(String) as attribute) Relationship are :
I don't really understand everything about what I code so every help is appreciated, I'm aware that my code is probably not very good at this point. I've followed this tutorial : https://www.youtube.com/watch?v=huRKU-TAD3g&t=6s (and made some changes to see if It solved my problem but it did not) Thank you :) MVVPapp.swift
ContentView.swift
AjoutSymptomeView.swift
|
|
@twoStraws cautions us to criticize ideas, not people. So I will try to be careful with my response. This site, HackingWithSwift, is a community where we try to help others on their path to becoming iOS developers. We follow and support a 100 Day program that @twoStraws has carefully crafted with beginner, intermediate, and advanced topics. @twoStraws asks that you program each day and follow the program. The forums are here to help answer questions to help those on this journey. I sense that you are not following @twoStraws' excellent 100 Day program. You watched a video by Nick Sarno (he's great, by the way), but you come here and dump a load of code, much of it not even related to the problem, and are asking for our help to debug your code from Nick's YouTube videos. Maybe this is the idea that I feel deserves criticism. Perhaps, instead, you might consider following the 100 Day program? When you reach the lessons on CoreData you'll have a solid understanding of Views, collecting data, and updating data models. This is what you really need, in my opinion. From those lessons, you'll know that Views don't update because "data are added in another view." This is not how Swift works. Instead, you would know that views are updated when their underlying structs are modified. This could be from an animation sequence, and update to an @State variable, or quite possibly by receiving notifications from @Published vars in a class instance. We could help you by pointing you to the right lesson in the 100 Days curriculum, or by asking which part of a lesson gave you trouble. I have two suggestions for you.
Good luck! |
|
thank you for your reply and sorry for my request. I´ve followed some Paul Hudson videos, especially the one on relationship, but I had to move on to Nick Sarno because I didn´t find a video on many to many reltationship from Paul Hudson and was not able to do it by myself. my code was working (data updating without problem) before that but still on one to many relationship. because I didn´t find many to many relationship video, i tried writing my code from scratch with the tutorial from nick Sarno to implement many to many. I did not followed the 100 days curriculum because I have some learning disability (sorry if it seems like it doesn´t make sense but I really struggle and i'm at loss of word to really explain the problem.) anyway, I know that i'm missing a lot of basic understanding of Swift. I'll try again doing the 100 days could you point me to the right lesson in the 100 days curriculum ? and again, I'm really sorry if I offended anyone with my request, that was not my intention at all. I apologize. (it´s not an excuse but I'm autistic so I may have misunderstood some basic rules and come off as rude - and english is not my main language so it doesn´t help) thank you for your reply and for taking the time explaining why this was not ok |
|
I didn't read through all your code. But from your problem description your code seems to work. It just doesn't update the UI. It has nothing to do with your relationships. SwiftUI updates the view when a value is changed. Basically, you need to reload the array with your data after saving and not just save it. You also could try to save your data in
For further information try: https://www.donnywals.com/appropriately-using-dispatchqueue-main/ But this doesn't necessarly update your array with your data. Perhaps someone here can explain it in more details than I can do it. |
|
Your AjoutSymptomeView view will be updated when one of the vars marked @ObservedObject publishes a change. In your case, the only @ObservedObject is vm, and its published vars are: @Published var symptomes @Published var traitements The values of those 2 vars are updated by the fetch requests performed in your funcs getSymptomes() and getTraitements(). If you update the SymptomeEntity or TraitementEntity tables, the symptomes and traitements are not automatically updated, hence the AjoutSymptomeView view is not updated. If you want to update the view after you add a symptome, you need to update symptomes by calling getSymptomes(). Likewise, after you add a traitement, you need to update traitements by calling getTraitements(). |
|
If you want more reading on Core Data, the book by Donny Wals is not always completey clear, but I believe it is the only core data book for Swift 5: https://www.donnywals.com For SwiftUI, if you want a more concentrated and intense introduction than Paul's books and tutorials, try the Stanford lectures. The SwiftUI explanations in the 2021 lectures is much more helpful than 2020. 2022 lectures may be released in a month or so. Scroll below the introduction on this page: https://cs193p.sites.stanford.edu |
|
hi, Bob has it about right, and i would suggest you also consider additional possibilities. (1) you might just use (2) you might consider having your view model update its two @Published arrays by using the Donny Wals book that Bob mentioned has information on how to hook up (3) more generally, be aware that if an S object is associated with a T object, changing an attribute on one of these will not automatically trigger any sort of update on the other. for example, if view A shows an @Observed object T and lists the names of all the associated S objects, changing an S object's name over in view B will not trigger an update back in view A. if this is the type of update your app seems to be missing, then when adding, updating, or deleting a S object, be sure to tell all of its associated T objects that they might want to know about it (if any views are displaying information about S objects associated with a T). in your example of deleting an S:
hope some of this helps, DMG |
|
(Don't hesitate to tell me to open a new subject if needed, thanx) Thanks to all for your replies. I've already tried getTraitement and getSymptome but It doesn't work, nor dispatchedQueue or ObjectWillChange so I suspect I'm missing something elsewhere.... So I rolled back to the previous version of my code, without MVVM. When closing the sheet, the data are updated without problem, but now I'm still stuck on another part (which originally made me do the next version) (My code is basically the same as this tutorial One-to-many relationships with Core Data, SwiftUI, and @FetchRequest) : I don't understand how can I add a newSymptome to an already existing traitement ? I thought I need to use a fetchRequest with predicate on the traitement I want the symptome to be added but I can't seemed to make it work. Any help or idea ? the new symptome : (working)
I selected a traitement from a list so :
but It has a wrappedName :
|
|
As you have defined relationships between
|
|
hi, @Greenamberred is right; when Xcode generates an extension on each of the S and T objects, defining each property as how do you find these? a quick way is to find a reference in code to an attribute of some entity, say, to you should find an interface that looks something like this ... in my case, for an entity X which has a to-many relationship to an entity named
hope that helps, DMG |
|
SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
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.