4

Apples documentation on NSManagedObject’s managedObjectContext method says:

This method may return nil if the receiver has been deleted from its context.

Does anyone know under what circumstances this method will return nil for a receiver that is deleted and under what circumstances it will still return the managed object context?

Background:
I override -didSave in order to move files that are referenced by a deleted managed object to the trash. After that, i have to clear the undo manager, because the deletion of this managed object can no longer be undone, as the files cannot be put back from the trash. So i will call:
[[[self managedObjectContext] undoManager] removeAllActions];
which will only work if -managedObjectContext does not return nil at this point.

MartinW
  • 4,966
  • 2
  • 24
  • 60
  • 2
    Experience suggests Apple may have meant that to be read as "This method may return nil, if the receiver has been deleted from its context." and simply poorly subedited it; hopefully someone will have a better answer than that though. – Tommy Aug 26 '14 at 18:26
  • @Tommy If there is a subtlety in the use of the comma, as a non native speaker, I do not get it :) Can you explain? Anyhow my concern was about the „may“ and in fact in my case, even when the managed object *is* deleted, the message always did answer the managed object context. – MartinW Aug 26 '14 at 18:57
  • As written your interpretation is correct — given that it's been deleted it may return `nil`. With the comma it would mean more that the method may return `nil` and will do so if the object has been deleted. So there'd have been no ambiguity in when you'll get `nil` with the comma. Sadly the lack of a comma leaves us with ambiguity. – Tommy Aug 26 '14 at 19:23
  • If it always seems to me nil to you, I'd speculate that it's *not* nil if the object is deleted from a child context and was in memory from a parent context or some such thing. However the docs are poorly updated with nested context info-- the fact that [`awakeFromInsert` is called multiple times with nested contexts](http://stackoverflow.com/questions/19893775/awakefrominsert-called-twice-with-nested-contexts) isn't mentioned anywhere. – stevesliva Aug 27 '14 at 04:16

1 Answers1

2

Absolutely! There are a number of scenarios where this can happen. The most common is that the application is retaining a reference to the NSManagedObject after it was deleted from it's NSManagedObjectContext.

The connection between an NSManagedObjectContext and an NSManagedObject is a weak one because of scenarios like this. The context observes and manages the life cycle of managed objects. The two are very much joined at the hip. When the object is deleted from the context, the next save will break that weak relationship between managed object and context. The deleted managed object's association with the managed object context is removed.

quellish
  • 21,123
  • 4
  • 76
  • 83
  • Are there any resources that can teach me at what moment exactly the connection get’s broken? For example there seems to be a difference if i delete a managed object in this two scenarios: (1) i add a managed object, i save the context, i delete the managed object, i save the context and (2) i add a managed object, i delete the managed object, i save the context. – MartinW Aug 30 '14 at 15:27