Skip to content

Using FEMMapping as NSMapTable key leads to duplicated objects #96

Closed

Description

@jcavar

Hi,
It new version, FEM uses FEMMapping instead entity name as before as NSMapTable key. When using recursive relationships and therefore different FEMMapping in child object and when processing child, it tries to get object from cache but since child mapping is different it can't get it and inserts new object.

Simple solution is I think just to use mapping.entityName as key.

Activity

  1. dimazen commented on May 19, 2017

    @dimazen
    Contributor

    Can you share code of the mapping that you're using as well as some sample JSON, please?

  2. dimazen commented on May 19, 2017

    @dimazen
    Contributor

    Side note about why I rewrote code to use FEMMapping as a key instead of entityName and why we can't go back: initially I was planning to use the same prefetch code for the Realm extension. However later it turned out that no prefetch required for the Realm. So why I didn't move the code back? In the next version I'm planning to deprecate entityName, and to leave simply objectClass for all of the cases. Using class instead of the entityName gives me ability to cache setter implementations and to speed up deserialization.

  3. jcavar commented on May 19, 2017

    @jcavar
    ContributorAuthor

    Here is simplified version:

    Chat:

    + (FEMMapping *)recursiveMapping {
        FEMMapping *mapping = [[FEMMapping alloc] initWithEntityName:@"WCTChat"];
        [mapping addAttribute:[WCTChat identifierAttribute]];
        mapping.primaryKey = @"identifier";
        return mapping;
    }
    
    + (FEMMapping *)defaultMapping {
        FEMMapping *mapping = [[FEMMapping alloc] initWithEntityName:@"WCTChat"];
        [mapping addAttribute:[WCTChat identifierAttribute]];
        [mapping addRelationshipMapping:[WCTMessage defaultMapping]
                            forProperty:@"lastMessage"
                                keyPath:@"lastMsg"];
        mapping.primaryKey = @"identifier";
        return mapping;
    }
    

    Message:

    + (FEMMapping *)defaultMapping {
        FEMMapping *mapping = [[FEMMapping alloc] initWithEntityName:@"WCTMessage"];
        [mapping addRelationshipMapping:[WCTChat recursiveMapping]
                            forProperty:@"chat"
                                keyPath:@"chat"];
        mapping.primaryKey = @"identifier";
        return mapping;
    }
    

    JSON:

    "chat" : {
        "idChat" : 1,
        "lastMsg" : {
            "chat" : { idChat" : 1 },
            "idMessage" : 345
        }
    }
    
  4. jcavar commented on May 19, 2017

    @jcavar
    ContributorAuthor

    Sure, objectClass should be fine as well.

  5. jcavar commented on May 19, 2017

    @jcavar
    ContributorAuthor

    And also, I know that we can't do much about it now, but I think it would be much nicer if there was bug fixes release before Realm support. We wanted to get temporary fix for #80 but had to include whole Realm support as well, which in the end it seems broke some of functionality.

  6. dimazen commented on May 19, 2017

    @dimazen
    Contributor

    Agree, that to rollout a bugfix release would be a better option. Anyway it is a little bit late.

  7. dimazen commented on May 19, 2017

    @dimazen
    Contributor

    I'll try to release a hotfix within 1.5 hours

  8. dimazen commented on May 19, 2017

    @dimazen
    Contributor

    Observation: @jcavar do you have an inverse relationship? It looks like that you're trying to map chat to the message that is already included into the chat property.

  9. added 7 commits that reference this issue on May 19, 2017
  10. 6 remaining items

  11. added 9 commits that reference this issue on May 20, 2017
  12. dimazen commented on May 20, 2017

    @dimazen
    Contributor

    @jcavar this issue has been fixed in the 1.2.1 release.
    Available in both Carthage and Pods (don't forget to update your repo)!

  13. jcavar commented on May 20, 2017

    @jcavar
    ContributorAuthor

    @dimazen thank you, that was fast 👍

    Yes, we have inverse relationship from message to chat.

  14. dimazen commented on May 20, 2017

    @dimazen
    Contributor

    @jcavar what I meant by my observation is that you probably don't need nested Chat mapping, since Message already included into the Chat and by using inverse relationship CoreData will be able to set a correct chat value on the message object.

    In any case thanks for your report!

  15. jcavar commented on May 20, 2017

    @jcavar
    ContributorAuthor

    Yes, that is good point, but we have explicitly disabled inverse relationship in CoreData editor for some reason.

  16. dimazen commented on May 20, 2017

    @dimazen
    Contributor

    Got it. Good luck with your project 👍

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