Skip to content

Recursive relationship to same object #81

Closed

Description

@jcavar

Hi,
We have the following JSON structure:

{
    "chat": 
    {
        "identifier": 1,
        "message": 
        {
            "identifier": 2,
            "chat": 
            {
                "identifier": 1
            }
        }
    }
}

We are using CoreData and we encountered problem when transforming this. Chat with identifier 1 is created twice. I think we identified why is this happening:

So the issue is in FEMDeserializer.m
- (id)_objectFromRepresentation:(NSDictionary *)representation mapping:(FEMMapping *)mapping allocateIfNeeded:(BOOL)allocate.
If object is not in cache, it is inserted and then filled from external representation. When filling it, inner chat is encountered and not found in cache and inserted as well. So we end up with 2 same chats.

The issue could be solved by caching object before calling fillObject.
I hope this makes sense? Is this something that FastEasyMapping should handle?

Activity

  1. jcavar commented on Mar 31, 2017

    @jcavar
    ContributorAuthor

    So we would need to set primary key and cache before fill object which would look something like this:

    - (id)_objectFromRepresentation:(NSDictionary *)representation mapping:(FEMMapping *)mapping allocateIfNeeded:(BOOL)allocate {
        id object = [self.store registeredObjectForRepresentation:representation mapping:mapping];
        if (!object && allocate) {
            object = [self.store newObjectForMapping:mapping];
        }
        
        if (!object) {
            return nil;
        }
    
        
        id primaryKey = representation[mapping.primaryKeyAttribute.keyPath];
        primaryKey = [mapping.primaryKeyAttribute mapValue:primaryKey];
        [object setValue:primaryKey forKey:mapping.primaryKey];
        
        if ([self.store canRegisterObject:object forMapping:mapping]) {
            [self.store registerObject:object forMapping:mapping];
        }
        
        if (_delegateFlags.willMapObject) {
            [self.delegate deserializer:self willMapObjectFromRepresentation:representation mapping:mapping];
        }
    
        [self _fillObject:object fromRepresentation:representation mapping:mapping];
    
        if (_delegateFlags.didMapObject) {
            [self.delegate deserializer:self didMapObject:object fromRepresentation:representation mapping:mapping];
        }
    
        return object;
    }
    
  2. dimazen commented on Mar 31, 2017

    @dimazen
    Contributor

    Hello, @jcavar! Thanks for your issue! It looks like that FEM can handle this easily. Anyway primary key mapping has some specific rules and thus can be extracted from the object fulfilling method.

    I'll fix it later today if it works for you :)

  3. jcavar commented on Mar 31, 2017

    @jcavar
    ContributorAuthor

    Well that was fast response 👍. Sure it works :)

  4. added a commit that references this issue on Apr 2, 2017
  5. dimazen commented on Apr 2, 2017

    @dimazen
    Contributor

    I'll roll out update with fix later this morning (third of April)

  6. dimazen commented on Apr 3, 2017

    @dimazen
    Contributor

    @jcavar you can now use 1.1.2 version that contains this fix.
    #80 postponed until 1.2 that I'm currently working on. It looks like that you gonna split your data into chunks, therefore I've decided to combine errors handling with new major release.

    ping me if you have more issues :)

  7. jcavar commented on Apr 3, 2017

    @jcavar
    ContributorAuthor

    Nice, thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions