HomeiOS Developmentios - SwiftData relationship deleteRule not being constant

ios – SwiftData relationship deleteRule not being constant


I’ve some issues with the deleteRule in SwiftData.
When deleting some dad or mum objects, the kid objects doesn’t delete if the deleteRule is .cascade.

I’ve the beneath fashions:

@Mannequin
remaining class Train {
    @Attribute(.distinctive) var id: UUID
    @Attribute(.distinctive) var title: String
    
    @Relationship(deleteRule: .cascade, inverse: WorkoutExercise.train)
    var workouts: [WorkoutExercise] = [WorkoutExercise]()
    
    init(id: UUID = .init(), title: String = "", workouts: [WorkoutExercise] = []) {
        self.id = id
        self.title = title
        self.workouts = workouts
    }
}
@Mannequin
remaining class Exercise {
    @Attribute(.distinctive) var id: UUID
    @Attribute(.distinctive) var title: String
    
    @Relationship(deleteRule: .cascade, inverse: WorkoutExercise.exercise)
    var workouts: [WorkoutExercise] = [WorkoutExercise]()
    
    init(id: UUID = .init(), title: String = "", workouts: [WorkoutExercise] = []) {
        self.id = id
        self.title = title
        self.workouts = workouts
    }
}
@Mannequin
remaining class WorkoutExercise {
    var id: UUID
    var train: Train?
    var exercise: Exercise?
    var units: [ExerciseSet] = [ExerciseSet]()
    
    init(id: UUID = .init(), train: Train, units: [ExerciseSet] = []) {
        self.id = id
        self.train = train
        self.units = units
    }
}
struct ExerciseSet: Identifiable, Codable {
    var id: UUID
    var weight: Int
    var reps: Int
    var relaxation: Int
    
    init(id: UUID = .init(), weight: Int, reps: Int, relaxation: Int) {
        self.id = id
        self.weight = weight
        self.reps = reps
        self.relaxation = relaxation
    }
}

When deleting some Train object, I count on all its workouts: [WorkoutExercise] childs to be additionally deleted after which these WorkoutExercise sort objects to be unlinked from their exercise: Exercise dad or mum, however doesn’t occur.
Additionally, once I delete some Exercise objects, I count on all its workouts: [WorkoutExercise] to be deleted because the deleteRule is .cascade.

I created 3 completely different tabs within the app to check this by itemizing all Exercise, all WorkoutExercise and all Train in these three completely different tabs to be able to see what knowledge continues to be out there within the database after deleting a few of these objects.

Does the truth that WorkoutExercise mannequin is a baby of each Exercise and Train fashions impacts the deleteRule?

Many thanks!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments