CoreData fails to copy data to parent NSManagedObjectContext on save

Originator:brenneke
Number:rdar://20667995 Date Originated:4/23/2015
Status:Open Resolved:
Product:iOS SDK Product Version:8.3
Classification:Bug/Crash/Dataloss Reproducible:Yes
 
Summary:

# tl;dr;

When using -[NSManagedObjectContext insertObject:] on an object created outside the context, it fails to copy the object attributes to any parent context.

# Rationale

We have a large import of fairly unstructured JSON data, and in order to determine if we need to insert new objects or just update existing ones, we need to partially create NSManagedObjects before inserting them.

This is done by passing nil to to the context parameter of -[NSManagedObject initWithEntity:insertIntoManagedObjectContext:] (See -[CDTPersistenceController insertString:intoContext:] for an example.

Once the object is partially constructed, we can determine if it's required or not, and then use -[NSManagedObjectContext insertObject:] to insert the object into the object graph if we want to keep it. At that point we also connect any outstanding relationships, etc.

# The Problem

This works great if the NSManagedObjectContext you are inserting into talks directly to the persistent store coordinator.  However, if it is a child context of another managed object context, the attributes of the new object do not get copied to the parent context.  If the object has any validation of it's attributes (say, non-null for example), the call to -[NSManagedObject save:] will fail.

# The Workaround

As I described earlier, writing to a new context that talks directly to the persistent store coordinator, and then merging those changes in on the save notification works as intended.

Steps to Reproduce:
Build and run the project on a device or in the simulator.  Press _Insert into Standalone Context_ to operate the workaround, where we are inserting into a parallel managed object context. This works great.  Press _Insert into Child/Main Context_ to demonstrate the bug and abort() on the context saving.

Expected Results:
using -[NSManagedObject initWithEntity:entity insertIntoManagedObjectContext:nil]; followed by -[NSManagedObjectContext insertObject:] works correctly, and saving a child context copies the attributes of the new object to it's parent context.

Actual Results:
If the context being inserted into has a parent, the attributes of the new object are not copied to the parent upon save, and the parent will fail to save when validating non-null attributes.

Version:
Xcode 6.3.1 on OSX 10.10.3, iOS 8.3

Notes:
example project also copied to https://github.com/mbrennek/insertObjectIntoContextFailure

Configuration:
Xcode 6.3.1 on OSX 10.10.3, iOS 8.3 SDK

Attachments:
'CoreDataSaveFailureTest.zip' was successfully uploaded.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!