Frage

I have spent the last few hours trying a number of different solutions to this problem by looking at similar similar problems that have been resolved on StackOverFlow threads.

However, I have hit a brick wall.

Some background.

Using Restkit 0.22 and Core Data to Persist 2 Objects and set up a relationship between them. The two Objects that I am modelling are as follows, Relationships are also included in the form of a One-One relationship between COAgendaItem and COMapItemLocation (The relationship that isn't working at present) and a One-Many between COMapItemLocation and COAgendaItem (I have not looked at mapping this yet):

-- COAgendaItem

enter image description here

-- COMapItemLocation

enter image description here

They have the Corresponding Attributes and Relationships enter image description here

The desired result is to have the core data relationship working alongside RestKit, i.e. The property agendaItemLocation accessible via code on a COAgendaItem object.

The JSON Response from the Server looks like this

enter image description here

The above JSON Response is the data required for a COAgendaItem object plus the primary key of a COMapItemLocation Object. I am having a problem mapping the location_id field which is the primary key of a COMapItemLocation. (I have the Response Descriptors and the retrieval code set up and it is working correctly, it has only become a problem since I attempted to map a relationship)

So far I have modified my RKEntityMapping to try and map the relationship.

+ (RKMapping *)agendaItemMapping{

RKEntityMapping *agendaItemMapping = [RKEntityMapping mappingForEntityForName:@"COAgendaItem" inManagedObjectStore:[[COPersistenceCoordinator sharedCOPersistenceCoordinator]restKitObjectStore]];
[agendaItemMapping addAttributeMappingsFromDictionary:@{@"id": @"agendaItemID",@"title": @"agendaItemTitle",@"description": @"agendaItemDescription",@"start": @"agendaItemStartTime",@"end": @"agendaItemEndTime",@"category": @"agendaItemCategory",@"location_id" : @"agendaItemLocation"}];
[agendaItemMapping addConnectionForRelationship:@"agendaItemLocation" connectedBy:@"location_id"];


return agendaItemMapping;

}

However this causes a crash on launch as shown below (Note, I have added the @"location_id" : @"agendaItemLocation" mapping for the foreign key in the default mapping as well, is this necessary)

enter image description here

I have tried modifying the [agendaItemMapping addConnectionForRelationship:@"agendaItemLocation" connectedBy:@"location_id"]; Statement with all the combinations I can think of both in single string and dictionary formats i.e. @{"agendaItemLocation" : @"location_id"} but whatever I try, it always ends up in the same error as before 'Cannot connect relationship: invalid attributes given for source entity 'COAgendaItem': location_id'.

I am completely stumped on this one, can anyone point anything that I have done wrong or need to do differently?

Thanks greatly for any help in advance! :)

War es hilfreich?

Lösung

You describe 2 different relationships, but really you just have one with an inverse. This is correct, it's just your description which is wrong (and thus probably your understanding of setting the relationship contents).

The connection cannot be made (and you get an error) because RestKit needs to know the id of the other object to find (to connect to). You're telling it to use location_id, but it needs to be a property on the class (usually transient), not an item in the JSON (because the connection is made after the JSON has been processed and gone).

Add a property to the entity and a mapping to place the location_id into it. Update the connection with the name of that property.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top