1

i want to insert a previously created NSManagedObject which has some string attributes into my NSManagedObjectContext via the insertObject method. This seems to work without error but when i reload the saved object again all my string attributes are null.

I have created my entity with:

[[NSManagedObject alloc] initWithEntity:description 
         insertIntoManagedObjectContext:nil];

Thanks for any constructive feedback.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Not enough information-- show us some code. There's no inherent reason why the string would be nil unless you never set a value for it. – Tom Harrington Dec 06 '12 at 18:21

1 Answers1

0

It is not advisable to insert a "previously created" managed object because it is much more robust to insert it during creation.

If you want to copy a managed object (i.e. "insert it again") effectively creating two instances of it in the persistent store, you will have to create a new one and then copy the all the attributes.

It is certainly more reasonable to insert the object into the context and then delete it if desired. It is just the much more intuitive and documented way to achieve what you want.

I strongly advise against creating a context-less managed object in your usage case!

miken32
  • 42,008
  • 16
  • 111
  • 154
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • That is not true. It's possible to create a managed object that is not inserted into any managed object context. In fact this is exactly what the code snippet in the question does. By passing `nil` as the context argument, you get a managed object that's not inserted in any context. You can later insert it into a context, provided that the entity exists in the model that the context is using. – Tom Harrington Dec 07 '12 at 17:10
  • OK, you are right. But that begs the question if this is an appropriate design pattern. Seeing that some attributes are not persisted correctly, probably not. – Mundi Dec 08 '12 at 12:12
  • I've used it for a discardable edit controller-- when the user asks to create a new entry, initialize a non-inserted managed object to store the data they enter. If they save changes, insert the object, and if they cancel, just discard the object. – Tom Harrington Dec 10 '12 at 18:16
  • Good case in point why this would not be necessary. It is obvious how to accomplish this without this roundabout way. – Mundi Dec 10 '12 at 21:05
  • @Tom Harrington: Thats exactly what i want to achieve. Edit an entity without context and if the user wants to save the modified entity insert it into my context. But after i save my context all string properties are stored as null. Why is that ? – Tristan Heitzinger Dec 13 '12 at 14:26
  • Why not just insert the entity into the context and delete if not used? Why all these hacks? See my edit. – Mundi Dec 13 '12 at 16:19
  • @TristanHeitzinger-- like I said, post some more code, there's not enough information. – Tom Harrington Dec 13 '12 at 20:38
  • @TomHarrington Consider removing the down-vote after my most recent message edit. – Mundi Dec 14 '12 at 07:16
  • @Mundi, I still disagree pretty strongly with the answer. I don't consider it to be "certainly more reasonable" to use the approach you describe. That's one approach, but there are others, and context-free managed objects are not by any means the "hacks" you describe them as being. – Tom Harrington Dec 14 '12 at 19:17
  • @TomHarrington Use my method and I guarantee that the string attributes will magically reappear. How is inserting into a nil context not a hack? Where is this documented? To be fair you should remove the down vote. – Mundi Dec 15 '12 at 13:03
  • @Mundi, you can't make that guarantee, you don't have enough information (since Tristan still hasn't posted code). Regarding the use of a nil context, this situation is the exact (indeed the *only*) reason why `NSManagedObjectContext` has an `insertObject:` method. That method can't be used in any other situation. Also, see Apple's **ThreadedCoreData** sample code for an example of using a nil MOC when creating an object. I haven't removed the downvote because the answer is still highly inaccurate on multiple points. – Tom Harrington Dec 17 '12 at 22:26
  • @TomHarrington I'm reproducing this same issue, using `initWithEntity: insertIntoManagedObjectContext :` with a `nil` MOC and then using `insertObject:` later to add it to the MOC. Leads to `nil` properties on the managed object. Passing a MOC in the initial `init..` instead immediately fixes the issue (as this answer suggests). I'll add some code to the original question. Perhaps due to [`awakeFromInsert` not being called](http://stackoverflow.com/a/3868651/1265393)? – pkamb May 18 '16 at 18:04