Search code examples
ioscore-datansmanagedobjectnsmanagedobjectcontext

NSManagedObjectContext insertObject - string stored as null


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.


Solution

  • 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!