Restkit - 只有子 ID 的映射

Posted

技术标签:

【中文标题】Restkit - 只有子 ID 的映射【英文标题】:Restkit - mapping having only child id 【发布时间】:2013-01-16 10:55:08 【问题描述】:

我正在尝试在我的 ios 应用程序中使用 RestKit 0.20 将 json 映射到核心数据对象。 我有两种类型的对象:用户和消息。用户有很多消息。消息具有指向用户的字段:'from' 和 'to'。 这是我的消息 json:

[
    
        "created_at":1358265482867,
        "to":"50ed7b4e63e9b80200000009",
        "note":"Hello",
        "from":"50ee98365315d00200000003",
        "_id":"50f57cee11f2b00200000016",
        "__v":0
    ,
    
        "created_at":1358265582867,
        "to":"50ed7b4e63e9b80200000009",
        "note":"Hello 2",
        "from":"50ee98365315d00200000003",
        "_id":"50f57cee11f2b00200000016",
        "__v":0
    
]

我的映射代码如下:

// Object mappings
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:managedObjectStore];
userMapping.identificationAttributes = @[@"userId"];
[userMapping addAttributeMappingsFromDictionary:@
     @"_id": @"userId",
     @"name": @"name",
     @"note": @"headline",
     @"photo_url": @"photoUrl"
 ];

RKEntityMapping *messageMapping = [RKEntityMapping mappingForEntityForName:@"Message" inManagedObjectStore:managedObjectStore];
messageMapping.identificationAttributes = @[@"messageId"];
[messageMapping addAttributeMappingsFromDictionary:@
     @"_id": @"messageId",
     @"note": @"note",
     @"created_at": @"createdAt",
     @"from": @"from",
     @"to" : @"to"
 ];

// !!! This code is ok if json contains entire object (but contains only id).
// [messageMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"from" toKeyPath:@"from" withMapping:userMapping]];
// [messageMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"to" toKeyPath:@"to" withMapping:userMapping]];

RKEntityMapping *messageReversedMapping = [messageMapping inverseMapping];
RKRequestDescriptor *requestDescription = [RKRequestDescriptor requestDescriptorWithMapping:messageReversedMapping objectClass:[Message class] rootKeyPath:nil];
[objectManager addRequestDescriptor:requestDescription];

RKResponseDescriptor *userResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:userMapping pathPattern:@"/users" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:userResponseDescriptor];

RKResponseDescriptor *messageResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:messageMapping pathPattern:@"/messages" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:messageResponseDescriptor];

型号代码:

@interface Message : NSManagedObject

@property (nonatomic, retain) NSString * note;
@property (nonatomic, retain) NSNumber * createdAt;
@property (nonatomic, retain) NSString * messageId;
@property (nonatomic, retain) User *from;
@property (nonatomic, retain) User *to;


@interface User : NSManagedObject

@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSString * headline;
@property (nonatomic, retain) NSString * lastName;
@property (nonatomic, retain) NSNumber * me;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * photoUrl;
@property (nonatomic, retain) NSString * userId;
@property (nonatomic, retain) Message *messagesFrom;
@property (nonatomic, retain) Message *messagesTo;

问题在于,在 json 中,字段“to”和“from”是用户的 id,而不是嵌套对象,因此应用程序崩溃。

我发现了类似的 SO 问题,但仍然无法正常工作。

Mapping nested IDs with RestKit in Xcode using Objective-C

Mapping relationships in RestKit through an array of IDs doesn't work

谁能帮我正确设置映射,这样 json 将只包含用户的 ID 而不是嵌套对象?

【问题讨论】:

【参考方案1】:

好的,我想通了。 API 文档给出了如何映射这种关系的示例: http://restkit.org/api/latest/Classes/RKConnectionDescription.html

我仍然需要将字段 'from' 和 'to' 映射到虚拟的 'fromUserId' 和 'toUserId' 但它可以工作。

此代码适用于我:

NSManagedObjectContext *context = ApplicationDelegate.managedObjectContext;
NSEntityDescription *messageEntity = [NSEntityDescription entityForName:@"Message" inManagedObjectContext:context];
NSRelationshipDescription *userRelationshipFrom = [messageEntity relationshipsByName][@"from"];
RKConnectionDescription *connectionUserMessageFrom = [[RKConnectionDescription alloc] initWithRelationship:userRelationshipFrom attributes:@ @"fromUserId": @"userId" ];
[messageMapping addConnection:connectionUserMessageFrom];

NSRelationshipDescription *userRelationshipTo = [messageEntity relationshipsByName][@"to"];
RKConnectionDescription *connectionUserMessageTo = [[RKConnectionDescription alloc] initWithRelationship:userRelationshipTo attributes:@ @"toUserId": @"userId" ];
[messageMapping addConnection:connectionUserMessageTo];

【讨论】:

以上是关于Restkit - 只有子 ID 的映射的主要内容,如果未能解决你的问题,请参考以下文章

Restkit - 子映射不继承其父映射?

Restkit/Core Data 关系映射,实体到相同类型的实体(父/子)

RestKit 对象映射将 ID 映射为完全不同的负数

RestKit 的词法解析器问题

参考 id 的 RestKit 映射对象

通过 ID 数组映射 RestKit 中的关系不起作用