Help with 2 way relationships with classes (SwiftData / cloudkit)

Hi

I’m having real problems trying to get a simple “to do” type app working with cloudkit. It works fine with SwiftData but as soon as I add CloudKit I get lots of “container not found errors “ which I think relates to the relationships between my classes. If I strip out the group sort order class it works fine.

I’ve stripped the app back to basics to test - I want to be able to add a “task” (task data) to a “group list “ (group data) also also store the position of the task in that list (group sort order) as there may be lots of tasks in a list. The same task could also be in a different group list with a different position in the list (so another entry and value in group sort order) .. why does the following not work??

any help appreciated!

//  TaskData.swift //  TaskOutApp //

import Foundation import SwiftData

`@Model class TaskData: Identifiable, Equatable { var id = UUID() var title: String = "No Title"     var isDone: Bool = false     var isToday: Bool = false     var creationDate: Date = Date()          var doneDate: Date = Date()     var todayDate: Date = Date()

    // Use an array of GroupSortOrder to maintain both group and sort order     var groupSortOrders: [GroupSortOrder]? = nil          init(id: UUID = UUID(), title: String = "No Title", isDone: Bool = false, isToday: Bool = false, creationDate: Date = Date(), doneDate: Date = Date(), todayDate: Date = Date(), groupSortOrders: [GroupSortOrder]? = nil) {         self.id = id         self.title = title         self.isDone = isDone         self.isToday = isToday         self.creationDate = creationDate         self.doneDate = doneDate         self.todayDate = todayDate         self.groupSortOrders = groupSortOrders     }          static func currentDateString() -> String {         let formatter = DateFormatter()         formatter.dateStyle = .short         formatter.timeStyle = .short         return formatter.string(from: Date())     }          static func == (lhs: TaskData, rhs: TaskData) -> Bool {         lhs.id == rhs.id     } }

@Model class GroupData: Identifiable {     var id = UUID()     var title: String = "no title"     var icon: String = "no icon"     var creationDate: Date = Date()     var task: [TaskData]? = []          init(id: UUID = UUID(), title: String, icon: String, creationDate: Date = Date(), task: [TaskData] = []) {         self.id = id         self.title = title         self.icon = icon         self.creationDate = creationDate         self.task = task     }          static func currentDateString() -> String {         let formatter = DateFormatter()         formatter.dateStyle = .short         formatter.timeStyle = .short         return formatter.string(from: Date())     } }

@Model class GroupSortOrder: Identifiable {     var id = UUID()     var group: GroupData? = nil     var sortOrder: Int = 0          init(id: UUID = UUID(), group: GroupData? = nil, sortOrder: Int = 0) {         self.id = id         self.group = group         self.sortOrder = sortOrder     } }`

The error message you provided, “container not found errors“, doesn't seem to indicate anything related to your data model. If you can provide full description of the error, and also how you trigger the error, I may be able to comment.

SwiftData's CloudKit integration is based on NSPersistentCloudKitContainer. You can also follow the following technotes to understand and debug any synchronization issue:

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

I'm getting 2 errors when I try add cloudkit..

The first because a direct relationship does not exist and the second is a container issue ...

error: Store failed to load. <NSPersistentStoreDescription: 0x3009c0ae0> (type: SQLite, url: file:///var/mobile/Containers/Data/Application/D931D996-869F-4CED-842E-D1373B0DE090/Library/Application%20Support/default.store) with error = Error Domain=NSCocoaErrorDomain Code=134060 "A Core Data error occurred." UserInfo={NSLocalizedFailureReason=CloudKit integration requires that all relationships have an inverse, the following do not: GroupData: task GroupSortOrder: group TaskData: groupSortOrders} with userInfo { NSLocalizedFailureReason = "CloudKit integration requires that all relationships have an inverse, the following do not:\nGroupData: task\nGroupSortOrder: group\nTaskData: groupSortOrders"; }

SwiftData/PersistentModel.swift:327: Fatal error: Unable to find a container with entity GroupSortOrder

My classes are as follows:

import SwiftData

@Model
class TaskData: Identifiable, Equatable {
    var id = UUID()
    var title: String = "No Title"
    var isDone: Bool = false
    var isToday: Bool = false
    var creationDate: Date = Date()
    
    var doneDate: Date = Date()
    var todayDate: Date = Date()

    // Use an array of GroupSortOrder to maintain both group and sort order
    var groupSortOrders: [GroupSortOrder]? = nil
    
    init(id: UUID = UUID(), title: String = "No Title", isDone: Bool = false, isToday: Bool = false, creationDate: Date = Date(), doneDate: Date = Date(), todayDate: Date = Date(), groupSortOrders: [GroupSortOrder]? = nil) {
        self.id = id
        self.title = title
        self.isDone = isDone
        self.isToday = isToday
        self.creationDate = creationDate
        self.doneDate = doneDate
        self.todayDate = todayDate
        self.groupSortOrders = groupSortOrders
    }
    
    static func currentDateString() -> String {
        let formatter = DateFormatter()
        formatter.dateStyle = .short
        formatter.timeStyle = .short
        return formatter.string(from: Date())
    }
    
    static func == (lhs: TaskData, rhs: TaskData) -> Bool {
        lhs.id == rhs.id
    }
}

@Model
class GroupData: Identifiable {
    var id = UUID()
    var title: String = "no title"
    var icon: String = "no icon"
    var creationDate: Date = Date()
    var task: [TaskData]? = []
    
    init(id: UUID = UUID(), title: String, icon: String, creationDate: Date = Date(), task: [TaskData] = []) {
        self.id = id
        self.title = title
        self.icon = icon
        self.creationDate = creationDate
        self.task = task
    }
    
    static func currentDateString() -> String {
        let formatter = DateFormatter()
        formatter.dateStyle = .short
        formatter.timeStyle = .short
        return formatter.string(from: Date())
    }
}



@Model
class GroupSortOrder: Identifiable {
    var id = UUID()
    var group: GroupData? = nil
    var sortOrder: Int = 0
    
    init(id: UUID = UUID(), group: GroupData? = nil, sortOrder: Int = 0) {
        self.id = id
        self.group = group
        self.sortOrder = sortOrder
    }
}


It looks as though was related to the class relationships - I still have some testing to do but I now don't have any errors so it's looking good ... for info this is my class setup which appears ok ->

import Foundation
import CloudKit
import SwiftData

@Model
class TaskData: Identifiable, Equatable {
    var id: UUID = UUID()
    var title: String = "No Title"
    var isDone: Bool = false
    var isToday: Bool = false
    var creationDate: Date = Date()
    var doneDate: Date = Date()
    var todayDate: Date = Date()

    // Define an optional relationship to GroupData
    var group: GroupData?

    // Define an optional relationship to GroupSortOrder
    var groupSortOrders: [GroupSortOrder]? // Optional array
    
    init(id: UUID = UUID(), title: String = "No Title", isDone: Bool = false, isToday: Bool = false, creationDate: Date = Date(), doneDate: Date = Date(), todayDate: Date = Date(), group: GroupData? = nil, groupSortOrders: [GroupSortOrder]? = nil) {
        self.id = id
        self.title = title
        self.isDone = isDone
        self.isToday = isToday
        self.creationDate = creationDate
        self.doneDate = doneDate
        self.todayDate = todayDate
        self.group = group
        self.groupSortOrders = groupSortOrders
    }

    static func == (lhs: TaskData, rhs: TaskData) -> Bool {
        lhs.id == rhs.id
    }
}

@Model
class GroupData: Identifiable {
    var id: UUID = UUID()
    var title: String = "No Title"
    var icon: String = "No Icon"
    var creationDate: Date = Date()
    
    // Define an optional relationship to TaskData with an inverse relationship to `group`
    @Relationship(inverse: \TaskData.group)
    var tasks: [TaskData]? // Optional array
    
    // Define an optional relationship to GroupSortOrder
    var groupSortOrders: [GroupSortOrder]? // Optional array
    
    init(id: UUID = UUID(), title: String = "No Title", icon: String = "No Icon", creationDate: Date = Date(), tasks: [TaskData]? = nil, groupSortOrders: [GroupSortOrder]? = nil) {
        self.id = id
        self.title = title
        self.icon = icon
        self.creationDate = creationDate
        self.tasks = tasks
        self.groupSortOrders = groupSortOrders
    }
}

@Model
class GroupSortOrder: Identifiable {
    var id: UUID = UUID()
    var sortOrder: Int = 0

    // Define an optional relationship to GroupData with an inverse relationship to `groupSortOrders`
    @Relationship(inverse: \GroupData.groupSortOrders)
    var group: GroupData?

    // Define an optional relationship to TaskData with an inverse relationship to `groupSortOrders`
    @Relationship(inverse: \TaskData.groupSortOrders)
    var task: TaskData?

    init(id: UUID = UUID(), group: GroupData? = nil, task: TaskData? = nil, sortOrder: Int = 0) {
        self.id = id
        self.group = group
        self.task = task
        self.sortOrder = sortOrder
    }
}
Help with 2 way relationships with classes (SwiftData / cloudkit)
 
 
Q