Fix to allow importing a single object into a to many relationship instead of crashing. #145

wants to merge 25 commits into from

4 participants


Handle var=object for a to many relationship. Currently the code crashes. The fix causes it to import the single object into the relationship.


Can you give me an example of how this crashes? I'd ideally like to make a test case for this, but I'm also curious since this hasn't crashed here for me in a while...


Added a new pair of functions to fetch an object by it's NSManagedObjectID

BrianDoig added some commits Mar 8, 2012
@BrianDoig BrianDoig Added error logging for functions. b3cd9ea
@BrianDoig BrianDoig Use a separate store coordinator for background threads. This will he…
…lp avoid lockups when resuming from local notifications.
@BrianDoig BrianDoig Merge Fix dfa3c33
@BrianDoig BrianDoig Adding back in error logging for import.
Adding back in findWithObjectID
@BrianDoig BrianDoig Fix crash on save during import. 768a8a6
@BrianDoig BrianDoig Added helper function to obtain a permanent ID for an object that was…
… just inserted
@BrianDoig BrianDoig Added some assert checking to give better indication as to why a fetc…
…h crashed. Now asserts at the point of breaking.
@BrianDoig BrianDoig Merge branch 'master' of 9f6e6e8
@BrianDoig BrianDoig Added functions to create an entity and obtain an permanent ID for it…
… at creation time. This is useful to create an object in a state ready to be modified by a save block.
@BrianDoig BrianDoig Added several create and save and find/create functions. bdea06a
@BrianDoig BrianDoig Merge branch 'master' of d489d64
@BrianDoig BrianDoig Cleaned up createAndSaveEntityInContext where the localContext was sh…
…adowing the context.

Added MR_findFirstOrCreateByPredicate which is implemented with createAndSaveEntityInContext.
MR_findOrCreateByAttribute is now implemented in terms of findFirstOrCreateByPredicate.
@BrianDoig BrianDoig Updated by Xcode c8c4e2e
@BrianDoig BrianDoig Simplify MR_createAndSaveEntityInContext 4d8900a
@BrianDoig BrianDoig Fix main thread background save d6bc90c
@BrianDoig BrianDoig Fix the crash on import properly by obtaining permanent IDs for the o…
…bjects added as relations.

Found out what the problem was. The key was in NSManagedObject+MagicalDataImport.m.

What was happening was if your data was complex enough and had enough nested objects you would end up with only some of the objects that were created being passed to obtainPermanentIDsForObjects:error: method.

These relationship entities did not have a permeant objectID which resulted in a crash on save when a save takes place on a child context.

This was not found by the test harness because it only tests MR_importFromObject. It never tests anything using MR_importFromArray: which uses -[MagicalRecord saveWithBlock:] which creates a child context.

The fix was to create an array that contains ALL the objects that were inserted. That array is then passed to obtainPermanentIDsForObjects:error: so that all the objects are given permanent IDs at one time. This also results in faster import because previously each imported object had a permanent ID assigned one at a time. This is a huge overhead for CoreData and was very slow. By passing all the objects in one call, the import time is much quicker than it previously was. An import of a complicated data file with many nested relationships 2 and 3 deep went from 9 seconds on device down to 3 seconds.


I try to use [Program MR_importFromArray:responseObject inContext:[NSManagedObjectContext MR_defaultContext]]; and still crashing.

-NSManagedObjectContext(MagicalSaves) MR_saveWithErrorCallback: -> Saving : Context *** MAIN THREAD ***


Hi Brian, thanks for this information and apologies for not acting sooner on it. I believe that this is being addressed by #540, which will be in the next update.

@tonyarnold tonyarnold closed this Aug 19, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment