TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

New Topic Section Needed for SwiftData, Xcode Beta - SwiftData Question

Forums > SwiftUI

First, I think we need a new Forum for SwiftData.

Second, as I understand it, SwiftData will infer an inverse relationship:

@Relationship(.cascade) var contancts: [Contact] = [Contact]()

The question is, how do we see that inverse relationship. In an applicaiton with many Entities this will become very cumbersome. CoreData provided the page to create and see the relationships together with inverses. I searched the Xcode 15 beta for something that would allow me to see the inverse relationships to no avail.

Has anyone been able to find a view into SwiftData relationships?

3      

Firstly - I think the forum does not need a category for SwiftData (It would like having one for CoreData, Canvas, WeatherKit etc).

Secondly - SwiftData is very new (2nd beta now only released) so you probably used more then most people, however as far as i know it built on CoreData so you could always fall back to that if you need more then SwiftData can do. A bit like drop back to UIKit if SwiftUI could not do it. I am sure that it will improve as time goes on.

3      

@TimD  

I suppose you'd just refer to your model code to see where you've set those relationships, for example here we have 2 models or entities named Contact and ContactDepartment.

@Model
final class Contact {
    var name: String
    var department: ContactDepartment?

    init(name: String) {
        self.name = name
    }
}

And then you would have a ContactModel. In this example, using .cascade will delete the contacts in the department.

@Model
final class ContactModel {
    @Attribute(.unique) var name: String
    @Relationship(deleteRule: .cascade, inverse: \Contact.department)
    var contacts = [Contact]()

    init(name: String) {
        self.name = name
    }
}

3      

Repling to my own post to demo Explicit Relationships in both classes of a relationship. I like to know at a glance that the attribute is part of a relationship.

Relationships in SwiftData are tricky and still evolving

IMPORTANT 2023-12-03 Only set the inverse relationship on one side, on one class, of the Example.

NOTE: The Optional "?" indicates that the attribute may be nil.

Remember to set appropriate Delete Rules for your situation, I set all below to nullify for consistency.

Example One to One: Country can have only One Capital City

Class with Only One: Country: capitalCity

@Relationship(deleteRule: .nullify) var capitalCity: City?

Class with Only One: City: country

@Relationship(deleteRule: .nullify) var country: Country?

Example One to Many: Company can have many Addresses

Class with only One: Address: company

@Relationship(deleteRule: .nullify) var company: Company?

Class with Many: Company: addresses

@Relationship(deleteRule: .nullify, inverse: \Address.company) var addresses: [Address]?

Example Many to Many: An Invoice can have many Receipts, and a Receipt can be split over many Invoices

Class with Many: Receipt - can only set inverse on one side!

@Relationship(deleteRule: .nullify) var invoices: [Invoice]?

Class with Many: Invoice: Inverse set here since it is more likely an Invoice will have more Receipts than having a Receipt split over many Invoices.

@Relationship(deleteRule: .nullify, inverse: \Receipt.invoices) var receipts: [Receipt]?

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

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

Reply to this topic…

You need to create an account or log in to reply.

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.