How to solve relationship issues with SwiftData / CloudKit?

I have the following model set up in my app which utilizes SwiftData and Cloudkit. A Project object has an array of Book objects. A Book object has an array of Reader objects. A Reader object has an array of Comment objects. A Comment object has a reference to a Book object:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Project
{
@Relationship(deleteRule: .cascade, inverse: Book.project) var books : [Book]?
...
}
class Book
{
@Relationship(deleteRule: .nullify, inverse: Reader.books) var readers : [Reader]?
var project: Project?
var comments: [Comment]?
...
}
class Reader
{
@Relationship(deleteRule: .cascade, inverse: Comment.reader) var comments: [Comment]?
var book: [Book]?
...
}
class Comment
{
@Relationship(deleteRule: .nullify, inverse: Book.comments) var book: Book?
var reader: Reader?
...
}
</code>
<code>class Project { @Relationship(deleteRule: .cascade, inverse: Book.project) var books : [Book]? ... } class Book { @Relationship(deleteRule: .nullify, inverse: Reader.books) var readers : [Reader]? var project: Project? var comments: [Comment]? ... } class Reader { @Relationship(deleteRule: .cascade, inverse: Comment.reader) var comments: [Comment]? var book: [Book]? ... } class Comment { @Relationship(deleteRule: .nullify, inverse: Book.comments) var book: Book? var reader: Reader? ... } </code>
class Project
{
    @Relationship(deleteRule: .cascade, inverse: Book.project) var books : [Book]?
    
    ...
}

class Book
{
    @Relationship(deleteRule: .nullify, inverse: Reader.books) var readers : [Reader]?
    
    var project: Project?
    var comments: [Comment]?
    
    ...
}

class Reader
{
    @Relationship(deleteRule: .cascade, inverse: Comment.reader) var comments: [Comment]?
    
    var book: [Book]?
    
    ...
}

class Comment
{
    @Relationship(deleteRule: .nullify, inverse: Book.comments) var book: Book?

    var reader: Reader?
    
    ...
}

One other note is that Project and Reader are both root model objects (the same Reader can appear in different projects). That’s why the Book classes’s readers property deleteRule is set to .nullify instead of .cascade.

I have no issue with Project, Book, and Reader, I’m able to output Lists using the data from these classes and can see CloudKit updating them on multiple devices when I add / remove items. However, CloudKit stops working when the Comment class includes its two relationships properties from above (book? and reader?). When I remove those two properties, then CloudKit works fine again. (Note: I’ve also removed any other bit of code from the project that references the Comment class so I know that it’s those two lines of code that are causing the problem.)

Is there a flaw in my model that’s causing this? The relationship to Comment and Reader seems simple enough, it’s the same type of relationship that I used between Project and Book, Book and Reader. The relationship with Comment and Book is a bit different, where I define one Book per Comment (and then need to include an array of Comments in the Book class to satisfy relationship requirement). The reason for this is because a Reader can have many different Comments across different Books and Projects, and need a way to filter them.

I’m loss as to what’s going on here and feel it’s due to a lack of a deeper understanding of CloudKit / SwiftData relationships, but am unable to find many resources on the issue. Any help would be appreciated.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật