0

I am having troubles implementing Relationships into my project. My project consists of two Entities: Item and List

Item has a one to one relationship with List and is non-optional (i.e an Item has to be related to a List)

List has a one to many relationship (i.e the list can consist of multiple Items)

My problems arise when I try to implement the assignment of the relationship. My main issue is that I don't know where two implement the assignment function. I have tried a few things but have either had problems with getting nil-values (most likely related to the ViewModels). I have decide not to post all my attempts and the corresponding errors in order to keep the post more readable and structured(instead I was thinking of posting them afterwards in response to an answer).

For context I made a GitHub repository: https://github.com/Standwerth/Standwerth-SwiftUI-Core-Data-Relationships-with-MVVM

I am using a CoreDataManager file and ViewModels to communicate the logic.

EDIT: After troubleshooting a for a really long time I believe I found what caused the problem. The problem was most likely that I by mistake checked List to be the Parent entity of Item.

I will let the post stay up so that if anyone runs in to the same problem they can double check to make sure they didn't make the same mistake as me. If moderators feel the need to delete the question feel free to.

  • 1
    This question has way to much code, please focus on the core issue with a clear explanation and no more code than is needed to reproduce or explain the issue. – Joakim Danielson Jul 30 '20 at 19:48
  • I know it has too much code but I didn't know what other way to explain it in. I really tried my best to shorten it down. I don't know if it is better to ask the question but not provide any context, in that case it might be too ambiguous as well – SthlmSwift Jul 30 '20 at 20:20
  • I get the impression that this question is about MVVM and to handle object relationships so not really related to Core Data or SwiftUI other than that is what you use for your implementation. Is this correct? – Joakim Danielson Jul 30 '20 at 20:24
  • It is about how to handle object relationships (of Core Data objects) in SwiftUI using MVVM. But yes, I would definently say that it has more to do with MVVM and object relationships if I interpreted what you said correctly. Although there are problems that a rise because of Core Data such as the one to many relationship being a NSSet which you have to convert (which might cause problems since the NSSet consists of NSManagedObject and not a ViewModel) – SthlmSwift Jul 30 '20 at 20:32
  • But aren't you mixing things up now, you have a relationship in your model layer between List and Item so shouldn't you have the same relationship in your view model layer? The AddListViewModel should have an array of AddItemViewModel or directly an array of Item) – Joakim Danielson Jul 31 '20 at 06:54
  • That's what I have been trying to do. But I don't seem to know exactly what to do and where. But whenever I have tried to add the relationship in the ViewModel layer I have gotten problems with nil-values. – SthlmSwift Aug 01 '20 at 12:36
  • I think I might be able to solve the problem, I have gotten rid of all the nil-values but I get a duplicate/new instance of List (with the same name). I probably have to look over all of my Viewmodels+ CRUD functions in order to implement uniqueness to each instance of a NSManagedObject – SthlmSwift Aug 02 '20 at 15:46

0 Answers0