GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

Core Data validateForDelete misbehavior

Forums > Swift

@kassi  

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      

Hacking with Swift is sponsored by try! Swift Tokyo.

SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!

Get your ticket here

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.