Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoCoding 2.2 + HRCoder does not encode relations into JSON #13

Open
fatuhoku opened this issue Mar 28, 2014 · 6 comments
Open

AutoCoding 2.2 + HRCoder does not encode relations into JSON #13

fatuhoku opened this issue Mar 28, 2014 · 6 comments

Comments

@fatuhoku
Copy link

Despite the changelog saying

Now supports @dynamic properties, allowing AutoCoding to be used with NSManagedObjects

relations (@dynamic NSSets) aren't encoded properly.

Reproduction

Make a simple model with two entities, with a To-One and a To-Many inverse relationships linking between them. Here, I've chosen Continent and Country as a simple domain.

screen shot 2014-03-28 at 11 39 01

Suppose I want to store a Continent named "Europe", that is related to a Country named "France". I've stored this in a View Controller property called self.continent below. I'm using HRCoder to encode the Continent object into JSON:

        // self.continent is "Europe", as described above
        NSData *data = [NSJSONSerialization dataWithJSONObject:[HRCoder archivedJSONWithRootObject:self.continent]
                                            options:NSJSONWritingPrettyPrinted
                                              error:&error];
        NSString *jsonRepresentation = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@", jsonRepresentation); // Output onto console!

Here, the JSON I get back is:

{
  "$class" : "MESContinent",
  "name" : "Europe"
}

... no sign of the related Country! Why's that?

Workaround

Use something like: https://gist.github.com/nuthatch/5607405
That particular solution doesn't preserve object references.

@nicklockwood
Copy link
Owner

Can you post your project somewhere, and I'll investigate. Might be a problem with HRCoder, or it could be that relationships need to be handled in a different way, or something.

@fatuhoku
Copy link
Author

Sure. Give me a moment to prepare that for you.

@fatuhoku
Copy link
Author

Okay, @nicklockwood I've pushed the demo project up to https://github.com/fatuhoku/autocoding-demo. It uses Cocoapods so be sure to pod install and it should run straight from the workspace.

It's just loads of tableviews at the moment, so it should be intuitive to use. You can create a named continent, and named countries (and named languages... but that doesn't matter a whole lot right now. They're made to relate to each other. See the JSON representation by touching the "Compose" button.

Thanks!

EDIT: I would totally ignore the BDDReactiveCocoaSpecs target — it's just sort of leftover cruft from my trying out HRCoder.

@fatuhoku
Copy link
Author

I would add that good test to make sure that AutoCoding+HRCoder handles relations recursively is if the JSON output of a Continent includes Language details. Continent here is the supposed 'root' of the object graph, after all.

@fatuhoku
Copy link
Author

I'll have a look into this as well. For the purposes of setting up testing for this you might find
http://www.cimgf.com/2012/05/15/unit-testing-with-core-data/ helpful.

@fatuhoku
Copy link
Author

I've found that AutoCoding does include the dynamic field, when I call

NSDictionary *dictionary = [europe dictionaryRepresentation];

However, when using HRCoder these fields don't come out in the JSON representation

@fatuhoku fatuhoku changed the title AutoCoding 2.2 does not encode relations AutoCoding 2.2 + HRCoder does not encode relations into JSON Mar 28, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants