0

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! :)

Jonathan Field
  • 263
  • 2
  • 8

1 Answers1

1

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.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Hi Wain, thank you for your help thus far. I have added a property inside of AgendaItem called locationID, now both Objects have an attribute with the same name and I modified the mapping to reflect this and it no longer crashes on launch. However I am now getting another error "Failed saving managed object context to the persistent store" , the Object shows that the .locationID property has been set but the property derived from the Relationship (COAgendaItem.agendaItemLocation) is showing as nil, any thoughts on what could be causing this? – Jonathan Field Feb 12 '14 at 20:33
  • What is the save error? Turn on trace logging for mapping. – Wain Feb 12 '14 at 21:16
  • Hi Wain, does this help? https://www.dropbox.com/s/ufeqvcato47dzsp/Screenshot%202014-02-12%2021.43.18.png – Jonathan Field Feb 12 '14 at 21:43
  • I guess you have the relation set to required? You may need to make it optional. – Wain Feb 12 '14 at 22:38
  • Hi Wain, thanks again for your help. When changed to optional I no longer get the error, but the agendaItemLocation (Property derived from the location is still nil). It seems like the relationship in core data is being completed ignored and that I am still going to need to write some code to find the location by ID and match it to the location_id field. It seems like the work done in Restkit so far isn't achieving anything if I have to write fetch code manually again? – Jonathan Field Feb 13 '14 at 08:20
  • You have updated the mapping to copy the location id, and updated the connection with the appropriate name? You should see it being used in the trace log. I expect you should not need to make it optional and you do not need to manually fetch (once the connection is setup correctly with RestKit). – Wain Feb 13 '14 at 08:30
  • Change the connection to `[agendaItemMapping addConnectionForRelationship:@"agendaItemLocation" connectedBy:@{ @"locationID": @"locationID" }];` – Wain Feb 13 '14 at 08:32
  • Hi Wain, that seems to have done the trick, the agendaItemLocation is still showing as nil for some reason but I think that might be a server side issue. I will mark the answer as accepted as you have solved the problem. Thanks! – Jonathan Field Feb 13 '14 at 10:16
  • Hi Wain, I haven't looked a this for a few days, I thought it was working at first but it still is not. I don't get the mapping error anymore but for the life of me I cannot get anything apart from a Null back on the agendaItemLocation attribute. I have used the addConnectionForRelationshipCode that you recommended 2 above. The only way I can get the the associated agendaItemLocation to function is to manually write a getter that grabs the mapItemLocation with the right location id but this totally defeats the point of the Rest Kit Relationship mapping surely? Any thoughts? – Jonathan Field Feb 21 '14 at 22:42
  • You'll need to turn on trace logging for the mapping and see what it says about the relationship – Wain Feb 21 '14 at 22:44
  • I turned it on with RKLogConfigureByName("RestKit/Relationships", RKLogLevelTrace); and it shows this in the console. For one of the Mapping Operations above where I have not set a relationship mapping it mentions a fault (Non related entity at this point), no explicit faults shown for COAgendaItem to COMapItemLocation mappings. Just shows as nil as pictured https://www.dropbox.com/s/wdqwikopz7kcg5f/Screenshot%202014-02-21%2022.53.20.png – Jonathan Field Feb 21 '14 at 22:54
  • Do all of the objects have `locationID` set? I guess so as you said you can connect them yourself. Are you using an object manager, or a mapping operation? – Wain Feb 21 '14 at 23:02
  • Hi Wain, yes all objects have a locationID set. I am using an Object Manager, with the mapping being set to the corresponding object mapping inside of the Response Descriptor. – Jonathan Field Feb 21 '14 at 23:10
  • Can you turn on trace logging for the whole mapping (not just relationship) and add the log (and the mappings) to a pastie ? – Wain Feb 21 '14 at 23:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48114/discussion-between-wain-and-jonathan-field) – Wain Feb 21 '14 at 23:41