0

I am saving objects in core data which is in the below format.

    {
        "propertyId": 1,
        "name": "some_name_1",
        "userName": "some_email_1"
    },
    {
        "propertyId": 1,
        "name": "some_name_2",
        "userName": "some_email_2"
    },
    {
        "propertyId": 2,
        "name": "some_name_3",
        "userName": "some_email_3"
    },
    {
        "propertyId": 2,
        "name": "some_name_4",
        "userName": "some_email_4"
    },
    {
        "propertyId": 3,
        "name": "some_name_5",
        "userName": "some_email_5"
    },
    {
        "propertyId": 3,
        "name": "some_name6_",
        "userName": "some_email_6"
    }

Now, the problem is that only the last object from the group of the same propertyId is getting saved in the Core Data.

This is the code I got from the previous developer. Mapping is used to store data.

RKEntityMapping *rkEntityMapping = [RKEntityMapping mappingForEntityForName:@"PropertyAssignee" inManagedObjectStore:self.managedObjectStore];
rkEntityMapping.identificationAttributes = @[@"propertyId"];

[rkEntityMapping addAttributeMappingsFromDictionary:@{
                                                      @"propertyId": @"propertyId",
                                                      @"name": @"name",
                                                      @"userName": @"userName"}];

[rkEntityMapping addConnectionForRelationship:@"property" connectedBy:@{@"propertyId": @"objId"}];

RKObjectMapping *syncMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];

[syncMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"propertyAssgineeResponse" toKeyPath:@"propertyAssgineeResponse" withMapping:propertAssigneeMapping]];

RKResponseDescriptor *responseDescriptorProperties = [RKResponseDescriptor responseDescriptorWithMapping:syncMapping method:RKRequestMethodAny pathPattern:@"/api/sync" keyPath:nil statusCodes:[NSIndexSet indexSetWithIndex:200]];

[self.objectManager addResponseDescriptor:responseDescriptorProperties];

I am not getting any clue of why this is happening.

Sneha
  • 2,200
  • 6
  • 22
  • 36
Rajan Kambaliya
  • 157
  • 1
  • 12
  • This is not a default behavior so you most likely do something in code to remove duplicate or you have setup constraints on propertyId. Check this post and see if this is what was being setup: https://stackoverflow.com/questions/21130427/how-to-add-unique-constraints-for-some-fields-in-core-data – Matic Oblak Oct 11 '17 at 06:32
  • The thing is that only the parameter "propertyId" will have same values, not "name" and "userName". They will be unique. The logic is that each property may be assigned to multiple users. – Rajan Kambaliya Oct 11 '17 at 06:37
  • You need to show your code, but my guess is that you are only creating a single `NSManagedObject` and updating that rather than creating a new `NSManagedObject` for each entry. – Paulw11 Oct 11 '17 at 06:38
  • @RajanKambaliya Then yet another option is you relations are not setup correctly. Check in your model to see if they are correctly set as one-to-many in your case or maybe even many-to-many. – Matic Oblak Oct 11 '17 at 06:40
  • I have added the code in updated question – Rajan Kambaliya Oct 11 '17 at 06:47
  • It's possible that the data model has a uniqueness constraint for the `propertyId` property. Check the entity declaration in the model editor. – Tom Harrington Oct 11 '17 at 14:49

0 Answers0