Finding all deleted objects from Core Data

  • Hi,

    I've a Core Data app where instances of a particular entity contain a
    filename. When these instances are removed from the object graph I
    also want to remove the file. But in order to support undo
    management, I can't directly remove the file, I've to wait until the
    managed object context is send a save: message. Now I've the problem
    that at that time I can only request the objects that will be deleted
    from the persistent store, not the objects that haven't been saved
    yet. How do I get a list of all objects that are deleted? The unod
    manager also doesn't seem to be able to give me a list of deleted
    objects.

    Thanks in advance,

    Remco Poelstra
  • Remco,

    What you can do is subclass NSManagedObject for that entity (if you haven't
    already done that) and add the -didSave method:

    - (void)didSave
    {
      if ([self isDeleted])
        [self _removeFile];

      [super didSave];
    }

    Kind regards,

    Diederik Hoogenboom

    Quoting Remco Poelstra  <remco...>:

    >
    > Hi,
    >
    > I've a Core Data app where instances of a particular entity contain a
    > filename. When these instances are removed from the object graph I
    > also want to remove the file. But in order to support undo
    > management, I can't directly remove the file, I've to wait until the
    > managed object context is send a save: message. Now I've the problem
    > that at that time I can only request the objects that will be deleted
    > from the persistent store, not the objects that haven't been saved
    > yet. How do I get a list of all objects that are deleted? The unod
    > manager also doesn't seem to be able to give me a list of deleted
    > objects.
    >
    > Thanks in advance,
    >
    > Remco Poelstra
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<mailings...>
    om

    >
    > This email sent to <mailings...>
    >
  • Hi,

    Thanks for your reply.
    This method doesn't solve my problem unfortunatly. This method (and
    willSave) are only called for objects which are allready in the
    persistent store or are going to end up there. If I have loaded an
    object graph and then create and delete an object and after that send
    a save: message, then did/willSave isn't called on that particular
    object. But it are exactly these objects I would like to find.

    Thanks again,

    Remco Poelstra

    Op 5-okt-2006, om 13:01 heeft <mailings...> het volgende
    geschreven:

    > Remco,
    >
    > What you can do is subclass NSManagedObject for that entity (if you
    > haven't
    > already done that) and add the -didSave method:
    >
    > - (void)didSave
    > {
    > if ([self isDeleted])
    > [self _removeFile];
    >
    > [super didSave];
    > }
    >
    >
    >
    > Kind regards,
    >
    > Diederik Hoogenboom
    >
    > Quoting Remco Poelstra  <remco...>:
    >
    >>
    >> Hi,
    >>
    >> I've a Core Data app where instances of a particular entity
    >> contain a  filename. When these instances are removed from the
    >> object graph I  also want to remove the file. But in order to
    >> support undo  management, I can't directly remove the file, I've
    >> to wait until the  managed object context is send a save: message.
    >> Now I've the problem  that at that time I can only request the
    >> objects that will be deleted  from the persistent store, not the
    >> objects that haven't been saved  yet. How do I get a list of all
    >> objects that are deleted? The unod  manager also doesn't seem to
    >> be able to give me a list of deleted  objects.
    >>
    >> Thanks in advance,
    >>
    >> Remco Poelstra
    >>
    >> _______________________________________________
    >> Do not post admin requests to the list. They will be ignored.
    >> Cocoa-dev mailing list      (<Cocoa-dev...>)
    >> Help/Unsubscribe/Update your Subscription:
    >> http://lists.apple.com/mailman/options/cocoa-dev/mailings%
    >> 40obviousmatter.com
    >>
    >> This email sent to <mailings...>
    >>
    >
    >
    >
  • Remco,

    Well in that case you can try registering for the
    NSManagedObjectContextObjectsDidChangeNotification notifaction. The userinfo
    dictionary of the notification object passed contains a key called
    NSDeletedObjectsKey which contains the NSManagedObject's being deleted.

    Kind regards,

    Diederik

    Quoting Remco Poelstra  <remco...>:

    >
    > Hi,
    >
    > Thanks for your reply.
    > This method doesn't solve my problem unfortunatly. This method (and
    > willSave) are only called for objects which are allready in the
    > persistent store or are going to end up there. If I have loaded an
    > object graph and then create and delete an object and after that send
    > a save: message, then did/willSave isn't called on that particular
    > object. But it are exactly these objects I would like to find.
    >
    > Thanks again,
    >
    > Remco Poelstra
    >
    > Op 5-okt-2006, om 13:01 heeft <mailings...> het volgende
    > geschreven:
    >
    >> Remco,
    >>
    >> What you can do is subclass NSManagedObject for that entity (if you  haven't
    >> already done that) and add the -didSave method:
    >>
    >> - (void)didSave
    >> {
    >> if ([self isDeleted])
    >> [self _removeFile];
    >>
    >> [super didSave];
    >> }
    >>
    >>
    >>
    >> Kind regards,
    >>
    >> Diederik Hoogenboom
    >>
    >> Quoting Remco Poelstra  <remco...>:
    >>
    >>>
    >>> Hi,
    >>>
    >>> I've a Core Data app where instances of a particular entity
    >>> contain a  filename. When these instances are removed from the
    >>> object graph I  also want to remove the file. But in order to
    >>> support undo  management, I can't directly remove the file, I've
    >>> to wait until the  managed object context is send a save: message.
    >>> Now I've the problem  that at that time I can only request the
    >>> objects that will be deleted  from the persistent store, not the
    >>> objects that haven't been saved  yet. How do I get a list of all
    >>> objects that are deleted? The unod  manager also doesn't seem to
    >>> be able to give me a list of deleted  objects.
    >>>
    >>> Thanks in advance,
    >>>
    >>> Remco Poelstra
    >>>
    >>> _______________________________________________
    >>> Do not post admin requests to the list. They will be ignored.
    >>> Cocoa-dev mailing list      (<Cocoa-dev...>)
    >>> Help/Unsubscribe/Update your Subscription:
    >>> http://lists.apple.com/mailman/options/cocoa-dev/mailings%
    >>> 40obviousmatter.com
    >>>
    >>> This email sent to <mailings...>
    >>>
    >>
    >>
    >>
    >
    >
  • Hi,

    do I need to do something more than calling [[NSNotificationCenter
    defaultCenter] addObserver:self
        selector:@selector(removeDatasheetFile:)
        name:@"NSManagedObjectContextObjectsDidChangeNotification"
        object:nil];
    } from awakeFromNib?
    Because my implementation of removeDatasheetFile doesn't seem to get
    called.

    Regards,

    Remco

    Op 5-okt-2006, om 16:20 heeft <mailings...> het volgende
    geschreven:

    > Remco,
    >
    > Well in that case you can try registering for the
    > NSManagedObjectContextObjectsDidChangeNotification notifaction. The
    > userinfo
    > dictionary of the notification object passed contains a key called
    > NSDeletedObjectsKey which contains the NSManagedObject's being
    > deleted.
    >
    > Kind regards,
    >
    > Diederik
    >
    > Quoting Remco Poelstra  <remco...>:
    >
    >>
    >> Hi,
    >>
    >> Thanks for your reply.
    >> This method doesn't solve my problem unfortunatly. This method
    >> (and  willSave) are only called for objects which are allready in
    >> the  persistent store or are going to end up there. If I have
    >> loaded an  object graph and then create and delete an object and
    >> after that send  a save: message, then did/willSave isn't called
    >> on that particular  object. But it are exactly these objects I
    >> would like to find.
    >>
    >> Thanks again,
    >>
    >> Remco Poelstra
    >>
    >> Op 5-okt-2006, om 13:01 heeft <mailings...> het
    >> volgende  geschreven:
    >>
    >>> Remco,
    >>>
    >>> What you can do is subclass NSManagedObject for that entity (if
    >>> you  haven't
    >>> already done that) and add the -didSave method:
    >>>
    >>> - (void)didSave
    >>> {
    >>> if ([self isDeleted])
    >>> [self _removeFile];
    >>>
    >>> [super didSave];
    >>> }
    >>>
    >>>
    >>>
    >>> Kind regards,
    >>>
    >>> Diederik Hoogenboom
    >>>
    >>> Quoting Remco Poelstra  <remco...>:
    >>>
    >>>>
    >>>> Hi,
    >>>>
    >>>> I've a Core Data app where instances of a particular entity
    >>>> contain a  filename. When these instances are removed from the
    >>>> object graph I  also want to remove the file. But in order to
    >>>> support undo  management, I can't directly remove the file,
    >>>> I've  to wait until the  managed object context is send a save:
    >>>> message.  Now I've the problem  that at that time I can only
    >>>> request the  objects that will be deleted  from the persistent
    >>>> store, not the  objects that haven't been saved  yet. How do I
    >>>> get a list of all  objects that are deleted? The unod  manager
    >>>> also doesn't seem to  be able to give me a list of deleted
    >>>> objects.
    >>>>
    >>>> Thanks in advance,
    >>>>
    >>>> Remco Poelstra
    >>>>
    >>>> _______________________________________________
    >>>> Do not post admin requests to the list. They will be ignored.
    >>>> Cocoa-dev mailing list      (<Cocoa-dev...>)
    >>>> Help/Unsubscribe/Update your Subscription:
    >>>> http://lists.apple.com/mailman/options/cocoa-dev/mailings%
    >>>> 40obviousmatter.com
    >>>>
    >>>> This email sent to <mailings...>
    >>>>
    >>>
    >>>
    >>>
    >>
    >>
    >
    >
    >
  • Hi,

    I solved the whole problem by overrinding the save: message and doing
    a fetch there, followed by a filtering of the objects I want to delete

    Kind regards,

    Remco Poelstra

    Op 5-okt-2006, om 17:38 heeft Remco Poelstra het volgende geschreven:

    > Hi,
    >
    > do I need to do something more than calling [[NSNotificationCenter
    > defaultCenter] addObserver:self
    > selector:@selector(removeDatasheetFile:)
    > name:@"NSManagedObjectContextObjectsDidChangeNotification"
    > object:nil];
    > } from awakeFromNib?
    > Because my implementation of removeDatasheetFile doesn't seem to
    > get called.
    >
    > Regards,
    >
    > Remco
    >
    > Op 5-okt-2006, om 16:20 heeft <mailings...> het
    > volgende geschreven:
    >
    >> Remco,
    >>
    >> Well in that case you can try registering for the
    >> NSManagedObjectContextObjectsDidChangeNotification notifaction.
    >> The userinfo
    >> dictionary of the notification object passed contains a key called
    >> NSDeletedObjectsKey which contains the NSManagedObject's being
    >> deleted.
    >>
    >> Kind regards,
    >>
    >> Diederik
    >>
    >> Quoting Remco Poelstra  <remco...>:
    >>
    >>>
    >>> Hi,
    >>>
    >>> Thanks for your reply.
    >>> This method doesn't solve my problem unfortunatly. This method
    >>> (and  willSave) are only called for objects which are allready in
    >>> the  persistent store or are going to end up there. If I have
    >>> loaded an  object graph and then create and delete an object and
    >>> after that send  a save: message, then did/willSave isn't called
    >>> on that particular  object. But it are exactly these objects I
    >>> would like to find.
    >>>
    >>> Thanks again,
    >>>
    >>> Remco Poelstra
    >>>
    >>> Op 5-okt-2006, om 13:01 heeft <mailings...> het
    >>> volgende  geschreven:
    >>>
    >>>> Remco,
    >>>>
    >>>> What you can do is subclass NSManagedObject for that entity (if
    >>>> you  haven't
    >>>> already done that) and add the -didSave method:
    >>>>
    >>>> - (void)didSave
    >>>> {
    >>>> if ([self isDeleted])
    >>>> [self _removeFile];
    >>>>
    >>>> [super didSave];
    >>>> }
    >>>>
    >>>>
    >>>>
    >>>> Kind regards,
    >>>>
    >>>> Diederik Hoogenboom
    >>>>
    >>>> Quoting Remco Poelstra  <remco...>:
    >>>>
    >>>>>
    >>>>> Hi,
    >>>>>
    >>>>> I've a Core Data app where instances of a particular entity
    >>>>> contain a  filename. When these instances are removed from the
    >>>>> object graph I  also want to remove the file. But in order to
    >>>>> support undo  management, I can't directly remove the file,
    >>>>> I've  to wait until the  managed object context is send a save:
    >>>>> message.  Now I've the problem  that at that time I can only
    >>>>> request the  objects that will be deleted  from the persistent
    >>>>> store, not the  objects that haven't been saved  yet. How do I
    >>>>> get a list of all  objects that are deleted? The unod  manager
    >>>>> also doesn't seem to  be able to give me a list of deleted
    >>>>> objects.
    >>>>>
    >>>>> Thanks in advance,
    >>>>>
    >>>>> Remco Poelstra
    >>>>>
    >>>>> _______________________________________________
    >>>>> Do not post admin requests to the list. They will be ignored.
    >>>>> Cocoa-dev mailing list      (<Cocoa-dev...>)
    >>>>> Help/Unsubscribe/Update your Subscription:
    >>>>> http://lists.apple.com/mailman/options/cocoa-dev/mailings%
    >>>>> 40obviousmatter.com
    >>>>>
    >>>>> This email sent to <mailings...>
    >>>>>
    >>>>
    >>>>
    >>>>
    >>>
    >>>
    >>
    >>
    >>
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<remco...>
    >
    > This email sent to <remco...>
  • Remco,

    The name: part calling the method should be
    name:NSManagedObjectContextObjectsDidChangeNotification, without the
    @"".

    Kind regards,

    Diederik

    On 5-okt-2006, at 17:38, Remco Poelstra wrote:

    > Hi,
    >
    > do I need to do something more than calling [[NSNotificationCenter
    > defaultCenter] addObserver:self
    > selector:@selector(removeDatasheetFile:)
    > name:@"NSManagedObjectContextObjectsDidChangeNotification"
    > object:nil];
    > } from awakeFromNib?
    > Because my implementation of removeDatasheetFile doesn't seem to
    > get called.
    >
    > Regards,
    >
    > Remco
    >
    > Op 5-okt-2006, om 16:20 heeft <mailings...> het
    > volgende geschreven:
    >
    >> Remco,
    >>
    >> Well in that case you can try registering for the
    >> NSManagedObjectContextObjectsDidChangeNotification notifaction.
    >> The userinfo
    >> dictionary of the notification object passed contains a key called
    >> NSDeletedObjectsKey which contains the NSManagedObject's being
    >> deleted.
    >>
    >> Kind regards,
    >>
    >> Diederik
    >>
    >> Quoting Remco Poelstra  <remco...>:
    >>
    >>>
    >>> Hi,
    >>>
    >>> Thanks for your reply.
    >>> This method doesn't solve my problem unfortunatly. This method
    >>> (and  willSave) are only called for objects which are allready in
    >>> the  persistent store or are going to end up there. If I have
    >>> loaded an  object graph and then create and delete an object and
    >>> after that send  a save: message, then did/willSave isn't called
    >>> on that particular  object. But it are exactly these objects I
    >>> would like to find.
    >>>
    >>> Thanks again,
    >>>
    >>> Remco Poelstra
    >>>
    >>> Op 5-okt-2006, om 13:01 heeft <mailings...> het
    >>> volgende  geschreven:
    >>>
    >>>> Remco,
    >>>>
    >>>> What you can do is subclass NSManagedObject for that entity (if
    >>>> you  haven't
    >>>> already done that) and add the -didSave method:
    >>>>
    >>>> - (void)didSave
    >>>> {
    >>>> if ([self isDeleted])
    >>>> [self _removeFile];
    >>>>
    >>>> [super didSave];
    >>>> }
    >>>>
    >>>>
    >>>>
    >>>> Kind regards,
    >>>>
    >>>> Diederik Hoogenboom
    >>>>
    >>>> Quoting Remco Poelstra  <remco...>:
    >>>>
    >>>>>
    >>>>> Hi,
    >>>>>
    >>>>> I've a Core Data app where instances of a particular entity
    >>>>> contain a  filename. When these instances are removed from the
    >>>>> object graph I  also want to remove the file. But in order to
    >>>>> support undo  management, I can't directly remove the file,
    >>>>> I've  to wait until the  managed object context is send a save:
    >>>>> message.  Now I've the problem  that at that time I can only
    >>>>> request the  objects that will be deleted  from the persistent
    >>>>> store, not the  objects that haven't been saved  yet. How do I
    >>>>> get a list of all  objects that are deleted? The unod  manager
    >>>>> also doesn't seem to  be able to give me a list of deleted
    >>>>> objects.
    >>>>>
    >>>>> Thanks in advance,
    >>>>>
    >>>>> Remco Poelstra
    >>>>>
    >>>>> _______________________________________________
    >>>>> Do not post admin requests to the list. They will be ignored.
    >>>>> Cocoa-dev mailing list      (<Cocoa-dev...>)
    >>>>> Help/Unsubscribe/Update your Subscription:
    >>>>> http://lists.apple.com/mailman/options/cocoa-dev/mailings%
    >>>>> 40obviousmatter.com
    >>>>>
    >>>>> This email sent to <mailings...>
    >>>>>
    >>>>
    >>>>
    >>>>
    >>>
    >>>
    >>
    >>
    >>
    >
  • On Oct 5, 2006, at 11:37 AM, Diederik Hoogenboom wrote:

    > The name: part calling the method should be
    > name:NSManagedObjectContextObjectsDidChangeNotification, without the
    > @"".

    This is a very good point, and another often-unstated rule of Cocoa
    development:

      Always use a global constant for notification, exception, and other
    names.

    You are not guaranteed that the value of that global will be identical
    to its name.  It could be anything, and it could even -- in theory --
    change from release to release.  Using the global will insulate you
    from these changes.

    It will also let you do things like command-double-click in Xcode and
    be taken to the constant's definition so you can see other possible
    constants and associated information.  For example, for a
    notification, error, or exception name, the header containing the
    constant declaration also often details what's in the userInfo
    property of the associated object at runtime.

      -- Chris
  • Ah, I see, I didn't even know that that constant was not to be
    interpreted as being a string.

    Thanks both for pointing that out to me!

    Remco Poelstra

    Op 5-okt-2006, om 20:44 heeft Chris Hanson het volgende geschreven:

    > On Oct 5, 2006, at 11:37 AM, Diederik Hoogenboom wrote:
    >
    >> The name: part calling the method should be
    >> name:NSManagedObjectContextObjectsDidChangeNotification, without
    >> the @"".
    >
    > This is a very good point, and another often-unstated rule of Cocoa
    > development:
    >
    > Always use a global constant for notification, exception, and
    > other names.
    >
    > You are not guaranteed that the value of that global will be
    > identical to its name.  It could be anything, and it could even --
    > in theory -- change from release to release.  Using the global will
    > insulate you from these changes.
    >
    > It will also let you do things like command-double-click in Xcode
    > and be taken to the constant's definition so you can see other
    > possible constants and associated information.  For example, for a
    > notification, error, or exception name, the header containing the
    > constant declaration also often details what's in the userInfo
    > property of the associated object at runtime.
    >
    > -- Chris
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<remco...>
    >
    > This email sent to <remco...>
  • Am 05.10.2006 um 17:38 schrieb Remco Poelstra:

    > do I need to do something more than calling [[NSNotificationCenter
    > defaultCenter] addObserver:self
    > selector:@selector(removeDatasheetFile:)
    > name:@"NSManagedObjectContextObjectsDidChangeNotification"
    > object:nil];

    Drop the quotes around the notification name. It's a constant, and
    the string it contains isn't guaranteed to be the same as the
    constant name (usually, Apple give it the same name but drop the "NS"
    part of it, but you shouldn't rely on that).

    Cheers,
    -- M. Uli Kusterer
    http://www.zathras.de
previous month october 2006 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
30 31          
Go to today