0

I need to relate entities using foreign id's downloaded form a web server. I have an event - category (one event has on category, and one category has many events). A web service to GET all categories, and a second web service to GET all events.

This are the json's returned by the web services:

categories:

[ 
 { "id" : "1",
    "name" : "music",
  },
  { "id" : "2",
    "name" : "sports",
  },
  { "id" : "3",
    "name" : "meetup"
  }
]

events:

[ 
  { "category_id" : "1",
    "description" : "free concert",
    "id" : "7791"
  },
  { "category_id" : "2",
    "description" : "Running event",
    "id" : "8357"
  }
 ]

I am saving category_id on my Events entity (RestKit mapping for both entities work fine).

Right now, to show the event's category I need to take the category_id and do a fetch request to core data, instead of using a getter (event.category.categoryID).

Is there a way to configure RestKit (and/or CoreData) to relate this two entities after doing a GET of the events? I'm using RestKit 0.20.3.

amcastror
  • 528
  • 5
  • 15

1 Answers1

0

My bad, I didn't go through the documentation enough. Here's how to do it (based on this stackoverflow's answer):

NSEntityDescription *eventEntity = [NSEntityDescription entityForName:NSStringFromClass([Event class]) inManagedObjectContext:store.mainQueueManagedObjectContext];
NSRelationshipDescription *categoryRelationship = [eventEntity relationshipsByName][@"category"];
RKConnectionDescription *connection = [[RKConnectionDescription alloc] initWithRelationship:categoryRelationship attributes:@{ @"categoryID": @"categoryID" }];

//entity mapping is the event's mapping like: RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName...
[entityMapping addConnection:connection];

The relationship "category" must be configured properly in Core Data.

For me that did the trick.

Community
  • 1
  • 1
amcastror
  • 528
  • 5
  • 15