RestKit与外键的多对多关系

时间:2014-03-19 07:21:57

标签: objective-c core-data restkit

我的模特中有多对多的关系。 当我从服务器获取对象时,不会存储关系。

如果我在代码中创建对象并向参与者添加旅程,那么每件事都是正确的。

我错过了什么?

参与者JSON:

participant: {
    event_id: 500,
    name: "Test",
    participant_id: 7900,
    tripIds: [
        5472,
        5678
    ]
}

Trip JSON:

trip: {
    participants: [
        7916,
        7900
    ],
    trip_id: 5472,
}

映射:

RKEntityMapping* participantMapping = [RKEntityMapping mappingForEntityForName:@"Participant" 
inManagedObjectStore:self.rkManagedObjectStore];
[participantMapping addAttributeMappingsFromDictionary:@{
                                                       @"participant_id": @"participantId",
                                                       @"name": @"name",
                                                       @"event_id": @"eventId",
                                                       @"tripIds":@"tripIds",
                                                       @"days":@"days"                                                           
                                                       }];

participantMapping.identificationAttributes = @[ @"participantId" ];
[participantMapping addConnectionForRelationship:@"event" connectedBy:@"eventId"];


NSEntityDescription *participantEntity = [NSEntityDescription entityForName:@"Participant" inManagedObjectContext:self.managedObjectContex];
NSRelationshipDescription *trips = [participantEntity relationshipsByName][@"participatedTrips"]; 
RKConnectionDescription *tripConnection = [[RKConnectionDescription alloc] initWithRelationship:trips attributes:@{ @"tripIds": @"tripId" }];


[participantMapping addConnection:tripConnection];


RKEntityMapping* tripMapping = [RKEntityMapping mappingForEntityForName:@"Trip" inManagedObjectStore:self.rkManagedObjectStore];
    [tripMapping addAttributeMappingsFromDictionary:@{
                                                      @"trip_id":@"tripId",
                                                      @"participants":@"participantIds"
                                                      }];
    tripMapping.identificationAttributes = @[ @"tripId" ];



    NSEntityDescription *tripEntity = [NSEntityDescription entityForName:@"Trip" inManagedObjectContext:self.managedObjectContex];
    NSRelationshipDescription *participants = [tripEntity relationshipsByName][@"participants"]; // To many relationship for the `User` entity
    RKConnectionDescription *connection = [[RKConnectionDescription alloc] initWithRelationship:participants attributes:@{ @"participantIds": @"participantId" }];


    [tripMapping addConnection:connection];

Print Object :(来自CoreData的对象,eventId是瞬态的)

(lldb) po participant
<Participant: 0xaf8a080> (entity: Participant; id: 0xaf8a390 <x-coredata://CFFD7472-B4AF-40BD-9E69-756BC26EB021/Participant/p79> ; data: {
    days = "(\n    4,\n    0\n)";
    event = "0xaf8dc70 <x-coredata://CFFD7472-B4AF-40BD-9E69-756BC26EB021/Event/p3>";
    eventId = 0;
    name = Test;
    participantId = 7900;
    participatedTrips = "<relationship fault: 0xafe6340 'participatedTrips'>";
    tripIds = "(\n    5472,\n    5678\n)";
})

0 个答案:

没有答案