Core Data Cascade delete rule sometimes doesn't

  • I have an "item" entity in a core data application with a to-many
    relationship to a second entity; the relationship's delete rule is set to
    "cascade", and the inverse (to-one) relationship's delete rule is set to
    nullify.
    I implemented a simple action in my application to delete a selected item,
    which invokes a "delete" method on item to do some cleanup before ultimately
    invoking deleteObject:self on the managed object context.  Everything works
    as expected; deleting the item causes the relationship's target objects to
    be deleted.

    I have just implemented an AppleScript command to delete an item, which
    invokes the same "delete" method on item.  However, when the operation is
    completes, the destination objects are not deleted (although the references
    in both directions are cleared).  This leaves my store in an invalid state
    because the target objects are not deleted and have no value for the inverse
    relationship back the item, which is not optional.

    I can't figure out any reason why this should behave differently in the two
    different circumstances.  No exceptions are being thrown, and I've stepped
    through the AppleScript deletion and it appears to complete normally.

    I have seen this problem in other contexts and coded around it (by deleting
    the target objects explicitly) under the assumption that I was doing
    something wrong.  This is the first case I've encountered where the same
    method works in one situation and fails in another.

    Any ideas?

    TIA,
    Doug K;

    P.S. I've considered filing a bug report, but I can't isolate this code, and
    I know the cascade rule works most of the time, so coding a simple test case
    will in all likelihood not expose the apparent bug.
  • On Feb 10, 2008, at 8:21 PM, Doug Knowles wrote:

    > I have an "item" entity in a core data application with a to-many
    > relationship to a second entity; the relationship's delete rule is
    > set to
    > "cascade", and the inverse (to-one) relationship's delete rule is
    > set to
    > nullify.
    > I implemented a simple action in my application to delete a selected
    > item,
    > which invokes a "delete" method on item to do some cleanup before
    > ultimately
    > invoking deleteObject:self on the managed object context.
    > Everything works
    > as expected; deleting the item causes the relationship's target
    > objects to
    > be deleted.
    >
    > I have just implemented an AppleScript command to delete an item,
    > which
    > invokes the same "delete" method on item.  However, when the
    > operation is
    > completes, the destination objects are not deleted (although the
    > references
    > in both directions are cleared).  This leaves my store in an invalid
    > state
    > because the target objects are not deleted and have no value for the
    > inverse
    > relationship back the item, which is not optional.
    >
    > I can't figure out any reason why this should behave differently in
    > the two
    > different circumstances.  No exceptions are being thrown, and I've
    > stepped
    > through the AppleScript deletion and it appears to complete normally.
    >
    > I have seen this problem in other contexts and coded around it (by
    > deleting
    > the target objects explicitly) under the assumption that I was doing
    > something wrong.  This is the first case I've encountered where the
    > same
    > method works in one situation and fails in another.
    >
    > Any ideas?
    >
    Have you tried manually calling "processPendingChanges" after the
    delete?  Cascade deletes are not processed immediately:

    http://developer.apple.com/releasenotes/Cocoa/CoreDataReleaseNotes/index.ht
    ml


    > TIA,
    > Doug K;
    >
    > P.S. I've considered filing a bug report, but I can't isolate this
    > code, and
    > I know the cascade rule works most of the time, so coding a simple
    > test case
    > will in all likelihood not expose the apparent bug.
  • Well, per the tech note, I do call processPendingChanges before I do the
    manual validation (which catches undeleted now-invalid objects).  And my
    understanding is that processPendingChanges is automatically called on a
    Save, which is where I first encountered the invalid records.
    The release note implies that Core Data is doing some work (like propagating
    deletions) at the top of the event loop; could it be that the processing of
    incoming AppleEvents is short-circuiting whatever Core Data normally does?

    On Feb 11, 2008 2:19 PM, Adam Swift <aswift...> wrote:

    > Have you tried manually calling "processPendingChanges" after the
    > delete?  Cascade deletes are not processed immediately:
    >
    >
    > http://developer.apple.com/releasenotes/Cocoa/CoreDataReleaseNotes/index.ht
    ml

    >
    >
previous month february 2008 next month
MTWTFSS
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29    
Go to today
MindNode
MindNode offered a free license !