RestKit关系映射多对多核数据关系故障错误 [英] RestKit Relation Mapping Many to Many Core Data relationship fault error

查看:102
本文介绍了RestKit关系映射多对多核数据关系故障错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用RestKit将以下JSON映射到核心数据。

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.



(实体:Hopper; id:0x767d240

(entity: Hopper; id: 0x767d240



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

所以我的用户都没有更新。

So none of my users are being updated.

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"
        }]
    }]
}

我的代码,它位于RestKit README中的Managed Object Request示例 https://github.com/RestKit/RestKit

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

我意识到我的NSURL需要改变的params,但The Hopper似乎映射确定:

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];}`


推荐答案

p>当调用 addPropertyMapping 并创建关系映射时, ... FromKeyPath 与JSON相关,设置为 @users

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

这篇关于RestKit关系映射多对多核数据关系故障错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆