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.

7
  • 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. Commented Jul 30, 2020 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 Commented Jul 30, 2020 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? Commented Jul 30, 2020 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) Commented Jul 30, 2020 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) Commented Jul 31, 2020 at 6:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.