gonzalezreal / Groot

From JSON to Core Data and back.
Other
534 stars 61 forks source link

Prevent infinite recursion when Groot traverses complex data model? #83

Open dhearst opened 7 years ago

dhearst commented 7 years ago

Hi,

We're trying to use Groot to serialize and deserialize our main data object class. It is a complex managed object with many relationships. In turn, many of its related objects are related to each other and to other nested objects. That means that there are 10-15 entities that are intertwined with forward and reverse relationships.

I've started testing Groot with our real-world data and immediately encountered a crash because Groot was recursing infinitely amidst our intertwined relationships. I have been able to prevent that by removing the JSONKeyPath user info on a few reverse relationships. This makes me wonder if I'm missing something in the way we've adopted Groot. I do have identityAttributes on all of our entities (applied to the entities themselves...is that correct?) Is there something else needed to convince Groot to stop parsing the object hierarchy when it reaches objects that were already parsed previously?

Thanks for any suggestions you can give me...Groot is amazing and we're really hoping to get it fully functional in our app. :)

David

gonzalezreal commented 7 years ago

Hi @dhearst,

In general, you should avoid trying to parse reverse relationships in the JSON. So removing the JSONKeyPath from entities in the right side of a relationship is a good idea.

Could you provide a sample of the JSON that is giving you trouble? Maybe I can help.

dhearst commented 7 years ago

Right now I'm trying to generate JSON from Core Data objects, so I have yet to generate the desired JSON output. From stepping through Groot with our data, I'm pretty sure our problems occur because of cyclic relationships, e.g.

A relates to B B relates to C C relates to A

As I step through the JSON-generation code, it appears that this leads to Groot repeatedly cycling through A, then B, then C, then back to A and eventually the app dies because of the depth of the recursion.

Is there a way to use Groot successfully in the presence of cyclic relationships like this? Ideally, we'd be able to parse only the unique objects in the data, skipping any relationship that points to an object previously parsed.

Or, is it better to leave off JSONKeyPath assignments for some/all of the potentially cyclic relationships and somehow recognize that they need to be reconnected after parsing the JSON to create objects?

Thanks, David

gonzalezreal commented 7 years ago

I am afraid Groot is not ready to work properly in the situation you are describing. As soon as I have a free slot available I will have a look and try to find a solution. Thanks for the feedback!

dhearst commented 7 years ago

OK, I've made some headway by being careful about which forward and/or reverse relationships have JSONParseKeys but I do think that we have a few cases where current Groot functionality may not be enough. I'm happy to provide details as needed if you get to the point where you are looking at this closely...I can do legwork or help out...just let me know. Thanks.

aymericbaur commented 6 years ago

Hello,

First, thank you for Groot, it's an amazing tool ! Unfortunately I am falling in the same recursive trap mentioned above. Is there a chance that you have any update on this problem ?

gonzalezreal commented 6 years ago

No update, sorry. I’m open to suggestions and pull requests 😀.