UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Core Data validateForDelete misbehavior

Forums > Swift

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()
}

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?

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.