UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Core Data relationship views

Forums > SwiftUI

I am struggling with how to represent data in a navigation view from a relationship in core data and how I should pull that data for my second view?

View A has a fetch request for a list of cardDecks. Each deck has as a "to many" relationship to Card (cards) within each deck. Each card only belongs to one deck. This view has a navigation link to the next to view that lists the cards in the deck

View B, what is the best standard to represent the card data? Perform a new FetchRequest with a predicate of the Deck or create a variable with an array of cards and pull the NSSet relationship from the card deck and make an array? The user will be able to create new cards or delete cards from the deck.

Or, am i missing another option that is preferred?

1) new FetchRequest with predicate for view B 2) create an array variable 3) ??

Dave

2      

hi Dave,

i would think that if ViewA shows a List of CardDecks, and if for each element in the List, you provide a NavigationLink with a destination of ViewB, then ViewB would want to know the CardDeck (i.e., ViewB should have a var deck: CardDeck).

ViewB then can find (and add as needed, or delete if you want) the Cards in the CardDeck's NSSet relationship (easily accessible by convertng to an Array of Card using, say, a computed variable you've added to the CardDeck class via extension). there would be no need to perform a separate Fetch -- you have all the Core Data references already available in the CardDeck.

(remember to make use of the accessors provided by XCode code generation to add Cards to or remove Cards from the NSSet relationship of the CardDeck.)

this should work for what you proposed. however there may still be one more shoe yet to drop in this story back in ViewA, depending on what your ViewA displays about each CardDeck (i'll monitor to see if anything arises).

hope that helps (for now),

DMG

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.