CoreData deletion validation error: "Items cannot be deleted from links."

Viewed 27

My Core Data Model has a 1-to-many relationship between two entities with deletion rule set to "Cascade" (and the inverse to Nullify).

Game
Relationship: links, Destination: WebLink, Inverse: game
Optional: true
Delete Rule: Cascade
Type: To Many

WebLink
Relationship: game, Destination Game, Invserse: links
Optional: true
Delete Rule: Nullify
Type: To One

In my list view I'm using .disabled(canDelete(item) == false) to dis/enable a swipe action which deletes the Game entity with the implementation

private func canDelete(_ item: T) -> Bool {
    do {
        try item.validateForDelete()
        return true
    } catch {
        return false
    }
}

and (for both, Game and WebLink).

public override func validateForDelete() throws {
    try super.validateForDelete()
    // more to come
}

When Game.links is empty, it works fine. However, when there are WebLink objects related to a game, the validation fails and I never get the delete action. The error is

Error Domain=NSCocoaErrorDomain Code=1600 "Items cannot be deleted from links." ...

and it's thrown from Game's super class. The WebLink.validateForDelete is not called in this case.

If I recall correctly, validateForDelete should only throw when the delete rule is "Deny", but it's actually "Cascade".

Now: if I hardcode a return true at the top of the canDelete function and then perform the deletion of the game object, it works fine, i.e. the game is deleted and changes are saved.

What am I doing wrong here?

0 Answers
Related