Core Data validateForDelete misbehavior

My Core Data Model has a 1-to-many relationship between 2 entities with deletion rule set to Cascade (and the inverse to Nullify). Game (links) --1-to-many--> WebLink (game, optional)

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 later
}

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?

This topic was automatically closed after 166 days. New replies are no longer allowed.