Yalantis / FastEasyMapping

A tool for fast serializing & deserializing of JSON

Home Page:https://yalantis.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error on non existing relationship

Ma-He opened this issue · comments

I've got a 'Person' entity with a relationship to a 'Location' entity. But in some cases a 'Person' entity can exists without a 'Location' entity. That's why my JSON model doesn't provide a 'location' key for certain persons.

When I try to deserialize a JSON 'Person' into a ManagedObject without a given location relationship, I get the following error:

*** Assertion failure in -[FEMCache addExistingObject:usingMapping:], /.../FEMCache.m:185
2014-09-08 15:30:47.906 AppName[7866:3191678] 
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'No value for key (locationId) on object (<Location: 0x1782d8330> (entity: Location; id: 0x178826480 <x-coredata:///Location/t149B2931-DA62-4EFC-8C7D-3CFC8058F57A16> ; data: {
city = nil;
country = nil;
houseNumber = nil;
latitude = nil;
locationId = nil;
longitude = nil;
relatedParkingSlot = nil;
streetName = nil;
zipCode = nil;
})) found'

Any suggestions on this?

@Ma-He
Hello there.

Can you share you mapping description and version?

I assume next case:
You've specified relationship at key "location" (let's assume")
Then Location entity get's created and passed to the cache. However, since there was no data in it - an empty Location is passed.

However, cache can't store objects without Primary Key.. so it crashes..

Hi,

yep, that's true. I've specified the location relationship as follows:

[mapping addRelationshipMapping:[self locationMapping]
                                                                   forProperty:PersonRelationships.location
                                                                       keyPath:PersonRelationships.location];

and my 'Location' mapping

+ (FEMManagedObjectMapping *)locationMapping
{
    return [FEMManagedObjectMapping mappingForEntityName:[Location entityName]
                                           configuration:^(FEMManagedObjectMapping *mapping) {
                                               [mapping addAttributeMappingDictionary:@{LocationAttributes.locationId : @"id",
                                                                                        LocationAttributes.houseNumber : @"streetNumber",
                                                                                        LocationAttributes.streetName : @"streetname"}];
                                               [mapping addAttributeMappingFromArray:@[LocationAttributes.city,
                                                                                       LocationAttributes.country,
                                                                                       LocationAttributes.latitude,
                                                                                       LocationAttributes.longitude,
                                                                                       LocationAttributes.zipCode]];

                                               mapping.primaryKey = LocationAttributes.locationId;
                                           }];
}

I'm using the current version 0.3.8

Basically, we don't have to create relationship if this key not found or represented as Null
Looks like a bug :)

That's just what I was thinking :-) First, I've assumed I've done something wrong with my mapping, but after some tests and different JSON representations it looks like a bug to me.
Thanks @poteryaysya for your help ;-)

@Ma-He
you can help me with pull request (if you'd like to!) ;)
i'll be ready closer to the end of the week

Having the same issue, had to add a null check.

@JacquesPienaar, @Ma-He
Just get back from my vacation.
I'll take a look in a few days

@Ma-He, @JacquesPienaar
Can you, guys, share JSON input for me?
Basically, it happens, when there is a mapping for some relationship, but no object presented in JSON, correct?

Correct

eg:

[mapping addToManyRelationshipMapping:[self promotion] forProperty:@"promotions" keyPath:@"promotions"];

json
"promotions": null

@JacquesPienaar thanks for help!

@JacquesPienaar
can reproduce it on to-one relationship, when key in JSON is missing.
it generate empty entity with nil fields. (which is obviously a bug)

@JacquesPienaar can you try 3e2f506?

Or give me some more info, because with NULL relationship it appears to be working correctly.
However with missing key it generated empty objects..

Resolved by #21

@Ma-He, @JacquesPienaar
I have some issues with my pod trunk session.
Meanwhile you can grab latest source by tag 0.4.0
I'll push it to the trunk as soon as permissions will be renewed

@Ma-He. @JacquesPienaar
0.4.1 should be available in Cocoapods, update repo :)