RestKit Relation Mapping Many to Many Core Data relationship fault error

时光毁灭记忆、已成空白 提交于 2020-01-03 04:32:20

问题


I am using RestKit to map the following JSON to core data.

I have a many-to-many relationship set up in core data.

A hopper can have many users and a user can have many hoppers.

I am getting this error message:

(entity: Hopper; id: 0x767d240

hopperToUsers = <relationship fault: 0x8352960 'hopperToUsers'  ;

So none of my users are being updated.

The json:

{
    "hoppers": [{
        "bearing": 0,
        "created_at": "2013-07-26T07:57:00Z",
        "distance": 0.0,
        "id": 4,
        "lat": "30.422032",
        "lng": "-86.617069",
        "name": "Fort Walton Beach",
        "updated_at": "2013-07-26T07:57:00Z",
        "users": [{
            "avatar_url": null,
            "created_at": "2013-07-26T21:37:21Z",
            "id": 1,
            "link": "http:/example.com/savetheday",
            "name": "Clark Kent",
            "post": " I once reported a heinous crime here.   Superman came to the rescue!",
            "thumbnail_url": null,
            "updated_at": "2013-07-26T21:37:21Z"
        }, {
            "avatar_url": null,
            "created_at": "2013-07-26T21:37:57Z",
            "id": 2,
            "link": "http:/example.com/villaincaught",
            "name": "Lex Luther",
            "post": " I got busted here. Almost took over the world!",
            "thumbnail_url": null,
            "updated_at": "2013-07-26T21:37:57Z"
        }]
    }]
}

My code, which is following the Managed Object Request example at the RestKit README https://github.com/RestKit/RestKit .

I realize my NSURL needs to be changed for the params, but The Hopper seems to map OK:

`[managedObjectStore createManagedObjectContexts];  
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:managedObjectStore];
[userMapping addAttributeMappingsFromDictionary:@{  @"avatar_url":          @"avatarURL"          ,
                                                      @"created_at":               @"createdAt",
                                                    @"link":                @"jsonURL",
                                                    @"post":                @"post",
                                                    @"thumbnail_url":       @"thumbnailURL",
                                                    @"updated_at":          @"updatedAt",
                                                    @"id":                  @"userID"}];
RKEntityMapping *hopperMapping = [RKEntityMapping mappingForEntityForName:@"Hopper"  inManagedObjectStore:managedObjectStore];
[hopperMapping addAttributeMappingsFromDictionary:@{   @"id":             @"hopperID",
                                                        @"lat":            @"lat",
                                                        @"lng":            @"lng",
                                                        @"name":           @"name",
                                                        @"created_at":     @"createdAt",
                                                        @"distance":        @"distance"}];

[hopperMapping addPropertyMapping:[RKRelationshipMapping     relationshipMappingFromKeyPath:@"hopperToUsers" toKeyPath:@"hopperToUsers" withMapping:userMapping]];

NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:hopperMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"hoppers" statusCodes:statusCodes];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:3000/hoppers.json?lat=30.422032&lng=-86.617069"]];
RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
operation.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;
operation.managedObjectCache = managedObjectStore.managedObjectCache;
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation,      RKMappingResult *result) {
    Hopper *hopper= [result firstObject];
    NSLog(@"Mapped the hopper: %@", hopper);
    NSLog(@"Mapped the user: %@", [hopper.hopperToUsers anyObject]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Failed with error: %@", [error localizedDescription]);
}];
NSOperationQueue *operationQueue = [NSOperationQueue new];
[operationQueue addOperation:operation];}`

回答1:


When you call addPropertyMapping and create the relationship mapping, the ...FromKeyPath relates to the JSON so it should be set to @"users".



来源:https://stackoverflow.com/questions/17910766/restkit-relation-mapping-many-to-many-core-data-relationship-fault-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!