Skip to content

`importFromArray` crashes #180

Closed
stranbird opened this Issue Jun 12, 2012 · 10 comments

7 participants

@stranbird

I used importFromArray:[NSArray arrayWithObjects:A1, A2, A3] to import a dictionary (with A has many B has many C), failed with a crash after saying

2012-06-12 07:48:00.211 cPresent[60713:1a403] -NSManagedObjectContext(MagicalSaves) MR_saveWithErrorCallback: -> Saving : Context

importFromArray:[NSArray arrayWithObjects:B1, B2, B3] failed either, while importFromArray:[NSArray arrayWithObjects:C1, C2, C3] works. Then i tried impprtFromObject with a single A, B and C, the result is positive.

After commenting lines related with obtainPermanentIDsForObjects and objectIDs, it works.

here is my json data:

[{"created_at":"2012-06-11T09:20:55Z","icon_url":"noun_project_1","id":1,"name":"Photo Gallery","updated_at":"2012-06-11T09:20:55Z","genus":[{"created_at":"2012-06-11T09:27:26Z","family_id":1,"icon_url":"http://img1.douban.com/view/photo/raw/public/p455618241.jpg","id":1,"name":"Fight Club","updated_at":"2012-06-11T09:27:26Z","species":[{"created_at":"2012-06-11T10:00:13Z","genu_id":1,"icon_url":"demo-s-0","id":1,"name":"Photo","updated_at":"2012-06-11T10:00:13Z"},{"created_at":"2012-06-11T10:00:13Z","genu_id":1,"icon_url":"demo-s-1","id":2,"name":"Photo","updated_at":"2012-06-11T10:00:13Z"},{"created_at":"2012-06-11T10:00:13Z","genu_id":1,"icon_url":"demo-s-2","id":3,"name":"Photo","updated_at":"2012-06-11T10:00:13Z"},{"created_at":"2012-06-11T10:00:13Z","genu_id":1,"icon_url":"demo-s-3","id":4,"name":"Photo","updated_at":"2012-06-11T10:00:13Z"},{"created_at":"2012-06-11T10:00:13Z","genu_id":1,"icon_url":"demo-s-4","id":5,"name":"Photo","updated_at":"2012-06-11T10:00:13Z"}]},{"created_at":"2012-06-11T09:40:34Z","family_id":1,"icon_url":"demo-g-0","id":2,"name":"\u51b0\u4e0e\u706b\u4e4b\u6b4c","updated_at":"2012-06-11T09:40:34Z","species":[]},{"created_at":"2012-06-11T09:40:34Z","family_id":1,"icon_url":"demo-g-1","id":3,"name":"Fight Club","updated_at":"2012-06-11T09:40:34Z","species":[]},{"created_at":"2012-06-11T09:40:34Z","family_id":1,"icon_url":"demo-g-2","id":4,"name":"Fight Club (ii)","updated_at":"2012-06-11T09:40:34Z","species":[]},{"created_at":"2012-06-11T09:40:34Z","family_id":1,"icon_url":"demo-g-3","id":5,"name":"The Social Network","updated_at":"2012-06-11T09:40:34Z","species":[]},{"created_at":"2012-06-11T09:40:34Z","family_id":1,"icon_url":"demo-g-4","id":6,"name":"The girl who kicked the bee's nest","updated_at":"2012-06-11T09:40:34Z","species":[]},{"created_at":"2012-06-11T09:40:34Z","family_id":1,"icon_url":"demo-g-5","id":7,"name":"Fight Club (iii)","updated_at":"2012-06-11T09:40:34Z","species":[]},{"created_at":"2012-06-11T09:40:34Z","family_id":1,"icon_url":"demo-g-6","id":8,"name":"The girl with the dragon tattoo","updated_at":"2012-06-11T09:40:34Z","species":[]},{"created_at":"2012-06-11T09:40:34Z","family_id":1,"icon_url":"demo-g-7","id":9,"name":"fight club (iv)","updated_at":"2012-06-11T09:40:34Z","species":[]},{"created_at":"2012-06-11T09:40:34Z","family_id":1,"icon_url":"demo-g-8","id":10,"name":"the black mirror","updated_at":"2012-06-11T09:40:34Z","species":[]}]},{"created_at":"2012-06-11T09:37:16Z","icon_url":"noun_project_2","id":2,"name":"Pricing and Offers","updated_at":"2012-06-11T09:37:16Z","genus":[]},{"created_at":"2012-06-11T09:37:16Z","icon_url":"noun_project_3","id":3,"name":"Photo Gallery","updated_at":"2012-06-11T09:37:16Z","genus":[]},{"created_at":"2012-06-11T09:37:16Z","icon_url":"noun_project_4","id":4,"name":"The Catalogues and Sales Methology","updated_at":"2012-06-11T09:37:16Z","genus":[]},{"created_at":"2012-06-11T09:37:16Z","icon_url":"noun_project_5","id":5,"name":"Publication","updated_at":"2012-06-11T09:37:16Z","genus":[]},{"created_at":"2012-06-11T09:37:16Z","icon_url":"noun_project_7","id":6,"name":"Presentation\u4ecb\u7ecd","updated_at":"2012-06-11T09:37:16Z","genus":[]},{"created_at":"2012-06-11T09:37:16Z","icon_url":"noun_project_8","id":7,"name":"Trainning Course","updated_at":"2012-06-11T09:37:16Z","genus":[]},{"created_at":"2012-06-11T09:37:16Z","icon_url":"noun_project_9","id":8,"name":"Video Gallery","updated_at":"2012-06-11T09:37:16Z","genus":[]},{"created_at":"2012-06-11T09:37:16Z","icon_url":"noun_project_1","id":9,"name":"Giti Tire","updated_at":"2012-06-11T09:37:16Z","genus":[]}]

@garnett
garnett commented Jun 12, 2012

The same for me: crash happens at 'enumerateObjects' line in + (NSArray *) MR_importFromArray:(NSArray *)listOfObjectData inContext:(NSManagedObjectContext *)context method

Crash message is EXC_BAD_ACCESS

@duanefields

Me too. The issue seems to be the fact that I'm importing data that is linked/related to any other data. In this case you can't import from object either.

MR_importFromObject:inContext: calls out to MR_primaryAttributeToRelateBy and returns nil, which it then passes on to try to build the lookup for existing objects. Since I don't have any relationships, it bombs. I guess you still need a way to identify for any given object what it's primary key is.

@duanefields

Found the issue. As described (maybe not quite clearly) here:

http://www.cimgf.com/2012/05/29/importing-data-made-easy/

You need to add an attributed to the entity (relatedByAttribute) to specify the primary key:

@stranbird

@duanefields I have added an the relatedByAttribute to indicate the primary key, and import one object with relations works fine. The problem occurs only when i using the importFromArray method.
So, maybe lack of primary key cause crash, but still other crash exist.

@jjaksic
jjaksic commented Aug 7, 2012

I'm experiencing the same type of crash - when saving an array of A where each A contains multiple B's. Importing just A's or just B's works fine, and importing A+B's one by one using MR_importFromObject also works fine. Like stranbird suggested, the problem is caused by obtainPermanentIDsForObjects.

I don't want to spend too much time analyzing why obtainPermanentIDsForObjects breaks the save, but I'm guessing it's one of the following:
1) obtainPermanentIDsForObjects only gets called with parent (A) objects and not with child (B) objects.
2) The object hierarchy of A+B's first gets constructed with temporary ids, then obtainPermanentIDsForObjects replaces ids of A objects while B objects still point to the old temporary A ids.

Anyhow, I think obtainPermanentIDsForObjects should simply be removed. I don't see why it's necessary or beneficial. It's only getting called during importFromArray and not from importFromObject, which, at best, is inconsistent, even if it didn't cause crashes.

@casademora
Magical Panda Software member
@jjaksic
jjaksic commented Aug 7, 2012

Thanks for the reply, Saul!
So, would you say that replicating MR_importFromArray without the obtainPermanentIDsForObjects is a good workaround for now? Or saving objects one by one using MR_importFromObject?

@casademora
Magical Panda Software member
@XBeg9
XBeg9 commented Sep 27, 2012

Hey! I have same crash for now, did someone find how fix this? Thanks.

@blackgold9
Magical Panda Software member

Have you tried the latest code? That should take care of this. Please respond if it didn't

@blackgold9 blackgold9 closed this Oct 15, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.